File tree Expand file tree Collapse file tree 4 files changed +146
-0
lines changed Expand file tree Collapse file tree 4 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution (object ):
8
+ def rotateRight (self , head , k ):
9
+ """
10
+ :type head: ListNode
11
+ :type k: int
12
+ :rtype: ListNode
13
+ """
14
+ if k == 0 :
15
+ return head
16
+ if not head :
17
+ return None
18
+
19
+ tempHead , length = head , 1
20
+ while tempHead .next :
21
+ length += 1
22
+ tempHead = tempHead .next
23
+
24
+ tempHead .next = head
25
+ jump = (length - k )% length
26
+
27
+ previous = tempHead
28
+ while jump > 0 :
29
+ previous = previous .next
30
+ jump -= 1
31
+ head = previous .next
32
+ previous .next = None
33
+
34
+ return head
Original file line number Diff line number Diff line change
1
+ '''
2
+ Validate if a given string is numeric.
3
+
4
+ Some examples:
5
+ "0" => true
6
+ " 0.1 " => true
7
+ "abc" => false
8
+ "1 a" => false
9
+ "2e10" => true
10
+
11
+ Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
12
+ '''
13
+
14
+ class Solution (object ):
15
+ def isNumber (self , s ):
16
+ """
17
+ :type s: str
18
+ :rtype: bool
19
+ """
20
+ s = s .strip ()
21
+ try :
22
+ if isinstance (float (s ),float ) or isinstance (int (s ),int ):
23
+ return True
24
+ except Exception as e :
25
+ return False
26
+
27
+ # Time: O(1)
28
+ # Space: O(1)
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
3
+
4
+ The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
5
+
6
+ You may assume the integer does not contain any leading zero, except the number 0 itself.
7
+
8
+ Example 1:
9
+
10
+ Input: [1,2,3]
11
+ Output: [1,2,4]
12
+ Explanation: The array represents the integer 123
13
+ '''
14
+
15
+ class Solution (object ):
16
+ def plusOne (self , digits ):
17
+ """
18
+ :type digits: List[int]
19
+ :rtype: List[int]
20
+ """
21
+ result = []
22
+ if not digits :
23
+ return []
24
+
25
+ carry = 1
26
+ new_digits = digits [::- 1 ]
27
+
28
+ for index in range (len (new_digits )):
29
+ new_digits [index ], carry = (new_digits [index ] + carry )% 10 , (new_digits [index ] + carry )/ 10
30
+
31
+ if carry > 0 :
32
+ new_digits .append (carry )
33
+ return new_digits [::- 1 ]
34
+
35
+ Time : O (N )
36
+ Space : O (1 )
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given two binary strings, return their sum (also a binary string).
3
+
4
+ The input strings are both non-empty and contains only characters 1 or 0.
5
+
6
+ Example 1:
7
+
8
+ Input: a = "11", b = "1"
9
+ Output: "100"
10
+
11
+ Example 2:
12
+
13
+ Input: a = "1010", b = "1011"
14
+ Output: "10101"
15
+ '''
16
+
17
+ class Solution (object ):
18
+ def addBinary (self , a , b ):
19
+ """
20
+ :type a: str
21
+ :type b: str
22
+ :rtype: str
23
+ """
24
+
25
+ result = ""
26
+
27
+ carry = 0
28
+ index_a , index_b = len (a )- 1 , len (b )- 1
29
+ while index_a >= 0 and index_b >= 0 :
30
+ result = (int (a [index_a ]) + int (b [index_b ]) + carry )% 2 + result
31
+ carry = (int (a [index_a ]) + int (b [index_b ]) + carry )% 2
32
+ index_a -= 1
33
+ index_b -= 1
34
+
35
+ if index_a >= 0 :
36
+ while index_a >= 0 :
37
+ result = (int (a [index_a ]) + carry )% 2 + result
38
+ carry = (int (a [index_a ]) + carry )% 2
39
+ index_a -= 1
40
+ elif index_b >= 0 :
41
+ while index_b >= 0 :
42
+ result = (int (b [index_b ]) + carry )% 2 + result
43
+ carry = (int (b [index_b ]) + carry )% 2
44
+ index_b -= 1
45
+ else :
46
+ if carry == 1 :
47
+ result = str (carry ) + result
48
+ return result
You can’t perform that action at this time.
0 commit comments