File tree Expand file tree Collapse file tree 4 files changed +190
-0
lines changed Expand file tree Collapse file tree 4 files changed +190
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
3
+
4
+ You should preserve the original relative order of the nodes in each of the two partitions.
5
+
6
+ Example:
7
+
8
+ Input: head = 1->4->3->2->5->2, x = 3
9
+ Output: 1->2->2->4->3->5
10
+ '''
11
+
12
+ # Definition for singly-linked list.
13
+ # class ListNode(object):
14
+ # def __init__(self, x):
15
+ # self.val = x
16
+ # self.next = None
17
+
18
+ class Solution (object ):
19
+ def partition (self , head , x ):
20
+ """
21
+ :type head: ListNode
22
+ :type x: int
23
+ :rtype: ListNode
24
+ """
25
+ if not head or not head .next :
26
+ return head
27
+
28
+ left , right = ListNode (0 ), ListNode (0 )
29
+ leftPtr , rightPtr = left , right
30
+
31
+ while head :
32
+ if head .val < x :
33
+ leftPtr .next = ListNode (head .val )
34
+ leftPtr = leftPtr .next
35
+ else :
36
+ rightPtr .next = ListNode (head .val )
37
+ rightPtr = rightPtr .next
38
+ head = head .next
39
+
40
+ if not left .next :
41
+ return right .next
42
+ elif not right .next :
43
+ return left .next
44
+ else :
45
+ leftPtr .next = right .next
46
+ return left .next
47
+ # Time: O(N)
48
+ # Space: O(N)
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
3
+
4
+ Below is one possible representation of s1 = "great":
5
+
6
+ great
7
+ / \
8
+ gr eat
9
+ / \ / \
10
+ g r e at
11
+ / \
12
+ a t
13
+
14
+ To scramble the string, we may choose any non-leaf node and swap its two children.
15
+
16
+ For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
17
+
18
+ rgeat
19
+ / \
20
+ rg eat
21
+ / \ / \
22
+ r g e at
23
+ / \
24
+ a t
25
+
26
+ We say that "rgeat" is a scrambled string of "great".
27
+ '''
28
+
29
+ class Solution (object ):
30
+ def __init__ (self ):
31
+ self .cache = {}
32
+
33
+ def isScramble (self , s1 , s2 ):
34
+ if s1 == s2 :
35
+ return True
36
+ if s1 + s2 in self .cache :
37
+ return self .cache [s1 + s2 ]
38
+ if len (s1 ) != len (s2 ) or sorted (s1 ) != sorted (s2 ):
39
+ self .cache [s1 + s2 ] = False
40
+ return False
41
+ for index in range (1 , len (s1 )):
42
+ if self .isScramble (s1 [:index ], s2 [:index ]) and self .isScramble (s1 [index :], s2 [index :]):
43
+ self .cache [s1 + s2 ] = True
44
+ return True
45
+ if self .isScramble (s1 [:index ], s2 [- index :]) and self .isScramble (s1 [index :], s2 [0 :- index ]):
46
+ self .cache [s1 + s2 ] = True
47
+ return True
48
+
49
+ self .cache [s1 + s2 ] = False
50
+ return False
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
3
+
4
+ Note: The solution set must not contain duplicate subsets.
5
+
6
+ Example:
7
+
8
+ Input: [1,2,2]
9
+ Output:
10
+ [
11
+ [2],
12
+ [1],
13
+ [1,2,2],
14
+ [2,2],
15
+ [1,2],
16
+ []
17
+ ]
18
+ '''
19
+
20
+ class Solution (object ):
21
+ def subsetsWithDup (self , nums ):
22
+ """
23
+ :type nums: List[int]
24
+ :rtype: List[List[int]]
25
+ """
26
+ result = [[]]
27
+ for num in nums :
28
+ for index in range (len (result )):
29
+ new_list = result [index ] + [num ]
30
+ new_list .sort ()
31
+ result .append (new_list )
32
+ unique = set (tuple (val ) for val in result )
33
+ return list (list (val ) for val in unique )
Original file line number Diff line number Diff line change
1
+ '''
2
+ A message containing letters from A-Z is being encoded to numbers using the following mapping:
3
+
4
+ 'A' -> 1
5
+ 'B' -> 2
6
+ ...
7
+ 'Z' -> 26
8
+
9
+ Given a non-empty string containing only digits, determine the total number of ways to decode it.
10
+
11
+ Example 1:
12
+
13
+ Input: "12"
14
+ Output: 2
15
+ Explanation: It could be decoded as "AB" (1 2) or "L" (12).
16
+
17
+ Example 2:
18
+
19
+ Input: "226"
20
+ Output: 3
21
+ Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
22
+ '''
23
+
24
+ class Solution (object ):
25
+ def numDecodings (self , s ):
26
+ """
27
+ :type s: str
28
+ :rtype: int
29
+ """
30
+ if not s or s [0 ] == '0' :
31
+ return 0
32
+ if len (s ) == 1 :
33
+ return 1
34
+
35
+ dp = [0 ]* len (s )
36
+ dp [0 ] = 1
37
+
38
+ if int (s [:2 ]) > 26 :
39
+ if s [1 ] != '0' :
40
+ dp [1 ] = 1
41
+ else :
42
+ dp [0 ] = 0
43
+ else :
44
+ if s [1 ] != '0' :
45
+ dp [1 ] = 2
46
+ else :
47
+ dp [1 ] = 1
48
+
49
+ for index in range (2 , len (s )):
50
+ if s [index ] != '0' :
51
+ dp [index ] += dp [index - 1 ]
52
+
53
+ val = int (s [index - 1 :index + 1 ])
54
+ if val >= 10 and val <= 26 :
55
+ dp [index ] += dp [index - 2 ]
56
+ return dp [len (s )- 1 ]
57
+
58
+ # Time: O(N)
59
+ # Space: O(N)
You can’t perform that action at this time.
0 commit comments