Skip to content

Commit c7c5be3

Browse files
committed
Add explanations for problem 3, 8, 151.
1 parent 9589403 commit c7c5be3

File tree

5 files changed

+102
-34
lines changed

5 files changed

+102
-34
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
| # | Title | Solution | Basic idea (One line) |
66
|---| ----- | -------- | ---------- |
7-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1_Two_Sum.py) | 1. hash 2. Sort and search with two points|
8-
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/15_3Sum.py) | 1. Sort and O(n^2) search with three points 2. Multiple Two Sum (Problem 1) |
7+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1_Two_Sum.py) | 1. hash <br>2. Sort and search with two points |
8+
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/3_Longest_Substring_Without_Repeating_Characters.py) |1. Check every possible substring O(n^2) <br>2. Remember the character index and current check pos, if character index >= current pos, then there is duplicate |
9+
| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/8_String_to_Integer(atoi).py) | Overflow, Space, and negative number |
10+
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/15_3Sum.py) | 1. Sort and O(n^2) search with three points <br>2. Multiple Two Sum (Problem 1) |
911
| 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/16_3Sum_Closest.py) | Sort and Multiple Two Sum check abs|
1012
| 18 | [4Sum](https://leetcode.com/problems/4sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/18_4Sum.py) | The same as 3Sum, but we can merge pairs with the same sum |
11-
| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/28_Implement_strStr().py) | 1. O(nm) comparison 2. KMP|
13+
| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/28_Implement_strStr().py) | 1. O(nm) comparison <br>2. KMP |
14+
| 65 | [Valid Number](https://leetcode.com/problems/valid-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/65_Valid_Number.py) | 1. strip leading and tailing space, then check float using exception, check e using split <br>2. check is number split by . or e, note that number after e may be negative |
1215
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/125_Valid_Palindrome.py)| Exclude non-alphanumeric characters and compare O(n) |
13-
||
16+
| 151 | [Reverse Words in a String](https://discuss.leetcode.com/category/159/reverse-words-in-a-string) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/151_Reverse_Words_in_a_String.py)| 1. Python split by space <br>2. Reverse all and reverse words|
Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
class Solution(object):
2+
# def reverseWords(self, s):
3+
# """
4+
# :type s: str
5+
# :rtype: str
6+
# """
7+
# # O(n) and O(n) space
8+
# if len(s) == 0:
9+
# return s
10+
# temp = s.split(' ')
11+
# temp = [t for t in temp if len(t) > 0]
12+
# temp = reversed(temp)
13+
# return ' '.join(temp)
14+
215
def reverseWords(self, s):
3-
"""
4-
:type s: str
5-
:rtype: str
6-
"""
7-
if len(s) == 0:
8-
return s
9-
temp = s.split(' ')
10-
temp = [t for t in temp if len(t) > 0]
11-
temp = reversed(temp)
12-
return ' '.join(temp)
16+
# remove tail space
17+
s = s.strip(' ')
18+
array_s = []
19+
last = ' '
20+
# remove multiple spaces
21+
for i in range(len(s)):
22+
if s[i] != ' ':
23+
array_s.append(s[i])
24+
else:
25+
if last != ' ':
26+
array_s.append(s[i])
27+
last = s[i]
28+
array_s = array_s[::-1]
29+
ls, pos = len(array_s), 0
30+
for i in range(ls + 1):
31+
if i == ls or array_s[i] == ' ':
32+
self.reverse(array_s, pos, i)
33+
pos = i + 1
34+
return ''.join(array_s)
35+
36+
def reverse(self, array_s, begin, end):
37+
for i in range((end - begin) / 2):
38+
array_s[begin + i], array_s[end - i - 1] = array_s[end - i - 1], array_s[begin + i]
39+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def containsNearbyAlmostDuplicate(self, nums, k, t):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:type t: int
7+
:rtype: bool
8+
"""

python/3_Longest_Substring_Without_Repeating_Characters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def lengthOfLongestSubstring(self, s):
6262
ls = len(s)
6363
i = max_len = 0
6464
for j in range(ls):
65+
# Note that when charMap[ord(s[j])] >= i, it means that there are
66+
# duplicate character in current i,j. So we need to update i.
6567
if charMap[ord(s[j])] >= i:
6668
i = charMap[ord(s[j])] + 1
6769
charMap[ord(s[j])] = j

python/65_Valid_Number.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
class Solution(object):
2+
# def isNumber(self, s):
3+
# """
4+
# :type s: str
5+
# :rtype: bool
6+
# """
7+
# # remove lead and tail space
8+
# s = s.strip()
9+
# try:
10+
# float(s)
11+
# return True
12+
# except:
13+
# if '.' in s or ' ' in s:
14+
# return False
15+
# temp = s.split('e')
16+
# if len(temp) == 2:
17+
# try:
18+
# int(temp[0])
19+
# int(temp[1])
20+
# except:
21+
# return False
22+
# return True
23+
# return False
24+
225
def isNumber(self, s):
3-
"""
4-
:type s: str
5-
:rtype: bool
6-
"""
7-
# remove lead and tail space
8-
s.strip()
9-
try:
10-
float(s)
26+
s = s.strip()
27+
ls, pos = len(s), 0
28+
if ls == 0:
29+
return False
30+
if s[pos] == '+' or s[pos] == '-':
31+
pos += 1
32+
isNumeric = False
33+
while pos < ls and s[pos].isdigit():
34+
pos += 1
35+
isNumeric = True
36+
if pos < ls and s[pos] == '.':
37+
pos += 1
38+
while pos < ls and s[pos].isdigit():
39+
pos += 1
40+
isNumeric = True
41+
elif pos < ls and s[pos] == 'e' and isNumeric:
42+
isNumeric = False
43+
pos += 1
44+
if pos < ls and (s[pos] == '+' or s[pos] == '-'):
45+
pos += 1
46+
while pos < ls and s[pos].isdigit():
47+
pos += 1
48+
isNumeric = True
49+
print pos, ls, isNumeric
50+
if pos == ls and isNumeric:
1151
return True
12-
except:
13-
if '.' in s or ' ' in s:
14-
return False
15-
temp = s.split('e')
16-
if len(temp) == 2:
17-
try:
18-
int(temp[0])
19-
int(temp[1])
20-
except:
21-
return False
22-
return True
2352
return False
24-

0 commit comments

Comments
 (0)