Skip to content

Commit f888d81

Browse files
committed
Solution of problems 117, 124, 127, 130, 140, 146, 150, 153, 162, 190, 212, 218, 239
1 parent edb07e6 commit f888d81

File tree

18 files changed

+668
-3
lines changed

18 files changed

+668
-3
lines changed

1-100q/10.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def isMatch(self, s, p):
2121
dp = [[False for _ in range(len(p) + 1)] for _ in range(len(s) + 1)]
2222
dp[0][0] = True
2323

24+
for index in range(1, len(dp[0])):
25+
if p[index-1] == '*':
26+
dp[0][index] = dp[0][index - 2]
27+
2428
for index_i in range(1, len(dp)):
2529
for index_j in range(1, len(dp[0])):
2630
if s[index_i - 1] == p[index_j - 1] or p[index_j - 1] == '.':

1-100q/8.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def myAtoi(self, str):
3+
"""
4+
:type str: str
5+
:rtype: int
6+
"""
7+
str = str.strip()
8+
number = ""
9+
10+
11+
for x in str:
12+
if x.isalpha() and number == "":
13+
return 0
14+
elif x.isalpha():
15+
break
16+
elif x == ".":
17+
break
18+
elif x == " ":
19+
break
20+
elif (x == "+" or x == "-") and number == "":
21+
number = number + x
22+
elif (x == "+" or x == "-") and number != "":
23+
break
24+
elif (x == "+" or x == "-") and (number[-1] == "+" or number[-1] == "-"):
25+
return 0
26+
elif (x == "+" or x == "-") and ("+" in number or "-" in number):
27+
break
28+
elif x.isdigit():
29+
number = number + x
30+
if number == "" or number == "+" or number == "-":
31+
return 0
32+
else:
33+
if int(number) > ((2**31)-1):
34+
return (2**31)-1
35+
elif int(number) < -(2**31):
36+
return -(2**31)
37+
else:
38+
return int(number)

100-200q/117.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,50 @@ def connect(self, root):
5555
queue.append(front.right)
5656
elif queue:
5757
queue.append(None)
58+
59+
60+
# Definition for binary tree with next pointer.
61+
# class TreeLinkNode:
62+
# def __init__(self, x):
63+
# self.val = x
64+
# self.left = None
65+
# self.right = None
66+
# self.next = None
67+
68+
class Solution:
69+
# @param root, a tree link node
70+
# @return nothing
71+
def connect(self, root):
72+
if not root:
73+
return None
74+
75+
root.next = None
76+
77+
while root:
78+
temp = root
79+
while temp:
80+
if temp.left:
81+
if temp.right:
82+
temp.left.next = temp.right
83+
else:
84+
temp.left.next = self.getNext(temp)
85+
if temp.right:
86+
temp.right.next = self.getNext(temp)
87+
88+
temp = temp.next
89+
if root.left:
90+
root = root.left
91+
elif root.right:
92+
root = root.right
93+
else:
94+
root = self.getNext(root)
95+
96+
def getNext(self, node):
97+
node = node.next
98+
while node:
99+
if node.left:
100+
return node.left
101+
if node.right:
102+
return node.right
103+
node = node.next
104+
return None

100-200q/124.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Given a non-empty binary tree, find the maximum path sum.
3+
4+
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
5+
6+
Example 1:
7+
8+
Input: [1,2,3]
9+
10+
1
11+
/ \
12+
2 3
13+
14+
Output: 6
15+
Example 2:
16+
17+
Input: [-10,9,20,null,null,15,7]
18+
19+
-10
20+
/ \
21+
9 20
22+
/ \
23+
15 7
24+
25+
Output: 42
26+
'''
27+
28+
# Definition for a binary tree node.
29+
# class TreeNode(object):
30+
# def __init__(self, x):
31+
# self.val = x
32+
# self.left = None
33+
# self.right = None
34+
35+
class Solution(object):
36+
def maxPathSum(self, root):
37+
"""
38+
:type root: TreeNode
39+
:rtype: int
40+
"""
41+
self.result = float('-inf')
42+
self.dfs(root)
43+
return self.result
44+
45+
def dfs(self, root):
46+
if not root:
47+
return 0
48+
49+
l = self.dfs(root.left)
50+
r = self.dfs(root.right)
51+
52+
max_one_end = max(max(l, r)+root.val, root.val)
53+
max_path = max(max_one_end, l+r+root.val)
54+
self.result = max(self.result, max_path)
55+
return max_one_end
56+
57+

100-200q/127.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def ladderLength(self, beginWord, endWord, wordList):
3939
d[s].append(word)
4040
else:
4141
d[s] = [word]
42-
42+
print d
4343
queue, visited = [], set()
4444
queue.append((beginWord, 1))
4545
while queue:
@@ -60,3 +60,5 @@ def ladderLength(self, beginWord, endWord, wordList):
6060
if neigh not in visited:
6161
queue.append((neigh, steps+1))
6262
return 0
63+
64+
Solution().ladderLength("hit", "cog", ["hot","dot","dog","lot","log","cog"] )

100-200q/130.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,36 @@ def solve(self, board):
2323
:type board: List[List[str]]
2424
:rtype: void Do not return anything, modify board in-place instead.
2525
"""
26-
26+
if len(board) == 0:
27+
return
28+
for row in range(len(board)):
29+
if board[row][0] == 'O':
30+
self.merge(board, row, 0)
31+
if board[row][len(board[0])-1] == 'O':
32+
self.merge(board, row, len(board[0])-1)
33+
34+
for col in range(len(board[0])):
35+
if board[0][col] == 'O':
36+
self.merge(board, 0, col)
37+
38+
if board[len(board)-1][col] == 'O':
39+
self.merge(board, len(board)-1, col)
40+
41+
for row in range(len(board)):
42+
for col in range(len(board[0])):
43+
if board[row][col] == 'O':
44+
board[row][col] = 'X'
45+
elif board[row][col] == '#':
46+
board[row][col] = 'O'
47+
48+
def merge(self, board, row, col):
49+
if row < 0 or col < 0 or row >= len(board) or col >= len(board[0]):
50+
return
51+
if board[row][col] != 'O':
52+
return
53+
54+
board[row][col] = '#'
55+
self.merge(board, row+1, col)
56+
self.merge(board, row, col-1)
57+
self.merge(board, row, col+1)
58+
self.merge(board, row-1, col)

100-200q/140.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def wordBreak(self, s, wordDict):
3+
"""
4+
:type s: str
5+
:type wordDict: List[str]
6+
:rtype: List[str]
7+
"""
8+
self.result = []
9+
self.dfs(s, wordDict, '')
10+
return self.result
11+
12+
def dfs(self, s, wordDict, currStr):
13+
if self.check(s, wordDict):
14+
if len(s) == 0:
15+
self.result.append(currStr[1:])
16+
for i in range(1, len(s)+1):
17+
if s[:i] in wordDict:
18+
self.dfs(s[i:], wordDict, currStr + ' ' + s[:i])
19+
20+
def check(self, s, wordDict):
21+
dp = [False for _ in range(len(s)+1)]
22+
dp[0] = True
23+
24+
for i in range(len(s)):
25+
for j in range(i, -1, -1):
26+
if dp[j] and s[j:i+1] in wordDict:
27+
dp[i+1] = True
28+
break
29+
30+
return dp[len(s)]

100-200q/146.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'''
2+
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
3+
4+
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
5+
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
6+
7+
Follow up:
8+
Could you do both operations in O(1) time complexity?
9+
10+
Example:
11+
12+
LRUCache cache = new LRUCache( 2 /* capacity */ );
13+
14+
cache.put(1, 1);
15+
cache.put(2, 2);
16+
cache.get(1); // returns 1
17+
cache.put(3, 3); // evicts key 2
18+
cache.get(2); // returns -1 (not found)
19+
cache.put(4, 4); // evicts key 1
20+
cache.get(1); // returns -1 (not found)
21+
cache.get(3); // returns 3
22+
cache.get(4); // returns 4
23+
24+
'''
25+
26+
class Node(object):
27+
def __init__(self, key, value):
28+
self.key = key
29+
self.value = value
30+
self.next = None
31+
self.prev = None
32+
33+
class LRUCache(object):
34+
35+
def __init__(self, capacity):
36+
"""
37+
:type capacity: int
38+
"""
39+
self.capacity = capacity
40+
self.mapping = dict()
41+
self.head = Node(0, 0)
42+
self.tail = Node(0, 0)
43+
self.head.next = self.tail
44+
self.tail.prev = self.head
45+
46+
47+
def get(self, key):
48+
"""
49+
:type key: int
50+
:rtype: int
51+
"""
52+
if key in self.mapping:
53+
node = self.mapping[key]
54+
self.remove(node)
55+
self.add(node)
56+
return node.value
57+
return -1
58+
59+
60+
def put(self, key, value):
61+
"""
62+
:type key: int
63+
:type value: int
64+
:rtype: void
65+
"""
66+
67+
if key in self.mapping:
68+
self.remove(self.mapping[key])
69+
70+
node = Node(key, value)
71+
if len(self.mapping) >= self.capacity:
72+
next_head = self.head.next
73+
self.remove(next_head)
74+
del self.mapping[next_head.key]
75+
76+
self.add(node)
77+
self.mapping[key] = node
78+
79+
def add(self, node):
80+
tail = self.tail.prev
81+
tail.next = node
82+
self.tail.prev = node
83+
node.prev = tail
84+
node.next = self.tail
85+
86+
def remove(self, node):
87+
prev_node = node.prev
88+
prev_node.next = node.next
89+
node.next.prev = prev_node
90+
91+
92+
93+
94+
# Your LRUCache object will be instantiated and called as such:
95+
# obj = LRUCache(capacity)
96+
# param_1 = obj.get(key)
97+
# obj.put(key,value)

0 commit comments

Comments
 (0)