Skip to content

Commit 8ac0f43

Browse files
Revise Folder Structure
1 parent 2497f0b commit 8ac0f43

File tree

261 files changed

+250
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

261 files changed

+250
-0
lines changed
File renamed without changes.
File renamed without changes.
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
3+
Description:
4+
5+
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
6+
7+
Example:
8+
9+
Input: [1,1,2]
10+
Output:
11+
[
12+
[1,1,2],
13+
[1,2,1],
14+
[2,1,1]
15+
]
16+
17+
'''
18+
19+
20+
class Solution:
21+
# @param num, a list of integer
22+
# @return a list of lists of integers
23+
24+
def permuteUnique(self, num):
25+
length = len(num)
26+
if length == 0: return []
27+
if length == 1: return [num]
28+
29+
# pre-processing
30+
31+
num.sort()
32+
res = []
33+
previousNum = None
34+
for i in range(length):
35+
if num[i] == previousNum: continue
36+
previousNum = num[i]
37+
for j in self.permuteUnique(num[:i] + num[i+1:]):
38+
res.append([num[i]] + j)
39+
return res
40+
41+
42+
def test_bench():
43+
44+
test_data = [1,1,2]
45+
46+
print( Solution().permuteUnique(test_data) )
47+
48+
return
49+
50+
51+
52+
if __name__ == '__main__':
53+
54+
test_bench()
File renamed without changes.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def pathSum(self, root, s):
10+
if not root:
11+
return []
12+
res = []
13+
stack = [(root, [root.val])]
14+
while stack:
15+
curr, ls = stack.pop()
16+
if not curr.left and not curr.right and sum(ls) == s:
17+
res.append(ls)
18+
if curr.right:
19+
stack.append((curr.right, ls+[curr.right.val]))
20+
if curr.left:
21+
stack.append((curr.left, ls+[curr.left.val]))
22+
return res
23+
24+
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def check_path_sum( self, node: TreeNode, sum, path: List[int]):
10+
11+
12+
13+
if node:
14+
15+
if node.val == sum and node.left is None and node.right is None:
16+
17+
cur_path = path + [ node.val ]
18+
19+
#print("{} is meet, return {}". format( node.val, str([cur_path]) ) )
20+
return True, [cur_path]
21+
22+
23+
else:
24+
25+
next_path = path + [ node.val ]
26+
27+
left_flag, left_sol = self.check_path_sum( node.left, sum-node.val, next_path)
28+
right_flag, right_sol = self.check_path_sum( node.right, sum-node.val, next_path)
29+
30+
path_container = [ ]
31+
32+
#print("\n\n current node value: {}".format(node.val))
33+
#print("left flag = {}, left sol={}".format( left_flag, left_sol) )
34+
#print("right flag = {}, right sol={}".format( right_flag, right_sol) )
35+
36+
if left_flag is True and right_flag is True:
37+
38+
path_container += [ p for p in left_sol ]
39+
path_container += [ p for p in right_sol ]
40+
41+
#print("return path container: {}".format( path_container) )
42+
return True, path_container[:]
43+
44+
elif left_flag is True:
45+
46+
sol_and_cur = [ p for p in left_sol ]
47+
#print("sol and cur: {}".format( sol_and_cur ) )
48+
49+
path_container += sol_and_cur
50+
51+
#print("return path container: {}".format( path_container) )
52+
return True, path_container[:]
53+
54+
elif right_flag is True:
55+
56+
sol_and_cur = [ p for p in right_sol ]
57+
#print("sol and cur: {}".format( sol_and_cur ) )
58+
59+
path_container += sol_and_cur
60+
61+
#print("return path container: {}".format( path_container) )
62+
return True, path_container[:]
63+
else:
64+
return False, []
65+
66+
else:
67+
# empty node or empty tree
68+
return False, []
69+
70+
71+
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
72+
73+
flag, solution = self.check_path_sum( root, sum, [] )
74+
75+
if flag:
76+
return solution
77+
else:
78+
return []
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def check_path_sum( self, node: TreeNode, sum, path: List[int], solution):
10+
11+
if node is None:
12+
# empty tree
13+
return
14+
15+
if node.val == sum and node.left is None and node.right is None:
16+
17+
# get one solution meets summation requirement
18+
# add current node to path
19+
path += [ node.val ]
20+
21+
# add current path to solution
22+
solution += [ path[:] ]
23+
24+
return
25+
26+
else:
27+
28+
# update sum for next level
29+
sum -= node.val
30+
31+
# add current node to path
32+
path += [ node.val ]
33+
34+
# dfs with left child
35+
self.check_path_sum( node.left, sum, path[:], solution)
36+
37+
# dfs with right child
38+
self.check_path_sum( node.right, sum, path[:], solution)
39+
40+
return
41+
42+
43+
44+
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
45+
46+
solution = []
47+
48+
self.check_path_sum( root, sum, [], solution )
49+
50+
return solution
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import deque
2+
3+
class Solution:
4+
def diStringMatch(self, S: str) -> List[int]:
5+
6+
numbers = deque( list( range( len(S)+1 ) ) )
7+
8+
output = []
9+
10+
for ch in S:
11+
12+
if ch == 'I':
13+
# Increaing, select min, and push it into output
14+
output.append( numbers.popleft() )
15+
16+
else:
17+
# Decreasing, select Max, and push it into output
18+
output.append( numbers.pop() )
19+
20+
21+
# last element
22+
output.append( numbers.pop() )
23+
24+
return output

0 commit comments

Comments
 (0)