Skip to content

Commit 585debd

Browse files
Chris WuChris Wu
authored andcommitted
no message
1 parent 7bd382c commit 585debd

21 files changed

+283
-45
lines changed

3sum.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def threeSum(self, nums):
4949
l+=1
5050
r-=1
5151
return res
52+
53+
"""
54+
I really take time to make the best solution, because I wanted to help people understand.
55+
If you like my answer, a star on GitHub I will really appreciated.
56+
https://github.com/wuduhren/leetcode-
57+
"""
5258

5359
"""
5460
def threeSum(self, nums):

add-binary.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#https://leetcode.com/problems/add-binary/
22
class Solution(object):
33
def addBinary(self, a, b):
4-
return bin(int(a, 2)+int(b, 2))[2:]
4+
return bin(int(a, 2)+int(b, 2))[2:]
5+
6+
"""
7+
I really take time to make the best solution, because I wanted to help people understand.
8+
If you like my answer, a star on GitHub I will really appreciated.
9+
https://github.com/wuduhren/leetcode-python
10+
"""

best-time-to-buy-an-stock.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ def maxProfit(self, prices):
1111
minPrice = price
1212
if (price-minPrice>maxProfit):
1313
maxProfit = price-minPrice
14-
return maxProfit
14+
return maxProfit
15+
16+
"""
17+
I really take time to make the best solution, because I wanted to help people understand.
18+
If you like my answer, a star on GitHub I will really appreciated.
19+
https://github.com/wuduhren/leetcode-python
20+
"""

binary-search-tree-iterator.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class BSTIterator(object):
9+
10+
def __init__(self, root):
11+
self.stack = []
12+
while root:
13+
self.stack.append(root)
14+
root = root.left
15+
16+
17+
18+
def next(self):
19+
node = self.stack.pop()
20+
r = node.right
21+
while r:
22+
self.stack.append(r)
23+
r = r.left
24+
return node.val
25+
26+
27+
28+
def hasNext(self):
29+
return len(self.stack)!=0
30+
31+
32+
33+
# Your BSTIterator object will be instantiated and called as such:
34+
# obj = BSTIterator(root)
35+
# param_1 = obj.next()
36+
# param_2 = obj.hasNext()
37+
38+
"""
39+
I really take time to make the best solution, because I wanted to help people understand.
40+
If you like my answer, a star on GitHub I will really appreciated.
41+
https://github.com/wuduhren/leetcode-python
42+
"""

course-schedule.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
we build a graph of adjacency list, like [0]
3+
0->[2,4,5]
4+
1->[3,4]
5+
meaning before taking 2,4,5 we need to take 0, before taking 3,4 we need to take 1
6+
if we find a loop back to itself then it is impossible, for example
7+
0->[2,4,5]
8+
1->[3,4]
9+
2->[0,3]
10+
0->2->0, which is imposible
11+
12+
now we iterate every course to see if it can loop back to itself in anyway [1]
13+
we do this by dfs and search for it self
14+
if we find itself we find loop
15+
16+
the time efficiency is O(V^2+VE), bc each dfs in adjacency list is O(V+E) and we do it V times
17+
space efficiency is O(E)
18+
V is the numCourses(Vertices)
19+
E is the prerequisites(Edges)
20+
"""
21+
class Solution(object):
22+
def canFinish(self, numCourses, prerequisites):
23+
graph = {n:[] for n in xrange(numCourses)} #[0]
24+
25+
for n1, n2 in prerequisites:
26+
graph[n1].append(n2)
27+
28+
for target_course in xrange(numCourses): #[1]
29+
stack = graph[target_course]
30+
visited = set()
31+
while stack:
32+
course = stack.pop()
33+
visited.add(course)
34+
if course==target_course: return False
35+
for ajc in graph[course]:
36+
if ajc not in visited:
37+
stack.append(ajc)
38+
return True

first-unique-character-in-a-string.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ def firstUniqChar(self, string):
1414
if counter[char]==1:
1515
return i
1616

17-
return -1
17+
return -1
18+
19+
"""
20+
I really take time to make the best solution, because I wanted to help people understand.
21+
If you like my answer, a star on GitHub I will really appreciated.
22+
https://github.com/wuduhren/leetcode-python
23+
"""

group-anagrams.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ def groupAnagrams(self, strs):
1111

1212
dic[tuple(string_count)].append(string)
1313

14-
return dic.values()
14+
return dic.values()
15+
16+
"""
17+
I really take time to make the best solution, because I wanted to help people understand.
18+
If you like my answer, a star on GitHub I will really appreciated.
19+
https://github.com/wuduhren/leetcode-
20+
"""

linked-list-cycle.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ def hasCycle(self, head):
88
slow = slow.next
99
if (fast==slow):
1010
return True
11-
return False
11+
return False
12+
13+
"""
14+
I really take time to make the best solution, because I wanted to help people understand.
15+
If you like my answer, a star on GitHub I will really appreciated.
16+
https://github.com/wuduhren/leetcode-python
17+
"""

longest-common-prefix.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ def longestCommonPrefix(self, strs):
1414
return ''
1515
else:
1616
return bench_mark[:i-1]
17-
return bench_mark
17+
return bench_mark
18+
19+
"""
20+
I really take time to make the best solution, because I wanted to help people understand.
21+
If you like my answer, a star on GitHub I will really appreciated.
22+
https://github.com/wuduhren/leetcode-python
23+
"""

longest-palindromic-substring.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
"""
2+
findPalindorome() find the palindrone string from the center "mid"
3+
The center could be one char (case 1) or two the same char (case 2)
4+
We iterate through every char in string and assume it is the center
5+
And put it in findPalindorome() to find the longest
6+
7+
Time Efficiency: O(N^2)
8+
Space Efficiency: O(N)
9+
"""
110
class Solution(object):
211
def longestPalindrome(self, s):
3-
4-
"""
5-
findPalindorome() find the palindrone string from the center "mid"
6-
The center could be one char (case 1) or two the same char (case 2)
7-
We iterate through every char in string and assume it is the center
8-
And put it in findPalindorome() to find the longest
9-
10-
Time Efficiency: O(N^2)
11-
Space Efficiency: O(N)
12-
"""
1312
def findPalindorome(mid, mid2=None):
1413
r = 0
1514
l = len(s)
@@ -42,5 +41,9 @@ def findPalindorome(mid, mid2=None):
4241
max_pal = p2 if len(p2)>len(max_pal) else max_pal
4342

4443
return max_pal
45-
44+
"""
45+
I really take time to explain my solution, because I wanted to help people understand.
46+
If you like my answer, a star on GitHub I will really appreciated.
47+
https://github.com/wuduhren/leetcode-python
48+
"""
4649

0 commit comments

Comments
 (0)