Posts

Showing posts with the label Python3

771. Jewels and Stones

You're given strings  J  representing the types of stones that are jewels, and  S  representing the stones you have.  Each character in  S is a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in  J  are guaranteed distinct, and all characters in  J  and  S  are letters. Letters are case sensitive, so  "a"  is considered a different type of stone from  "A" . Example 1: Input: J = "aA", S = "aAAbbbb" Output: 3 Example 2: Input: J = "z", S = "ZZ" Output: 0 Note: S  and  J  will consist of letters and have length at most 50. The characters in  J  are distinct. Code: class Solution:     def numJewelsInStones(self, J, S):         """         :type J: str         :type S: str         :rtype: int   ...

561. Array Partition I

Given an array of  2n  integers, your task is to group these integers into  n  pairs of integer, say (a 1 , b 1 ), (a 2 , b 2 ), ..., (a n , b n ) which makes sum of min(a i , b i ) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4). Note: n  is a positive integer, which is in the range of [1, 10000]. All the integers in the array will be in the range of [-10000, 10000]. Python 3 Code: class Solution:     def arrayPairSum(self, nums):         """         :type nums: List[int]         :rtype: int         """         nums.sort()         x = 0         for i in range (0,len(nums),2):             x += nums[i]         ret...

728. Self Dividing Numbers

A  self-dividing number  is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because  128 % 1 == 0 ,  128 % 2 == 0 , and  128 % 8 == 0 . Also, a self-dividing number is not allowed to contain the digit zero. Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible. Example 1: Input: left = 1, right = 22 Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22] Note: The boundaries of each input argument are  1 <= left <= right <= 10000 . Python 3 code ( Your runtime beats 87.86 % of python3 submissions. ) : class Solution:     def selfDividingNumbers(self, left, right):         """         :type left: int         :type right: int         :rtype: List[int]         """       ...

657. Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to  the original place . The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are  R  (Right),  L (Left),  U  (Up) and  D  (down). The output should be true or false representing whether the robot makes a circle. Example 1: Input: "UD" Output: true Example 2: Input: "LL" Output: false Python3 Code: class Solution:     def judgeCircle(self, moves):         """         :type moves: str         :rtype: bool         """         if moves.count("U") == moves.count("D") and moves.count("L") == moves.count("R"):             return True         else:   ...

13. Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Python 3 Code (By Wake Liu): class Solution:     def romanToInt(self, s):         """         :type s: str         :rtype: int         """         roman = {'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1}         ans = roman[s[-1]]         for i in range(len(s)-1):             ans += roman[s[i]]*((roman[s[i]]>=roman[s[i+1]])*2-1)         return ans