Skip to content

Commit 661bd26

Browse files
committed
7_Reverse_Integer.py
186_Reverse_Words_in_a_String_II
1 parent 87a0bbc commit 661bd26

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66

77
| # | Title | Solution | Basic idea (One line) |
88
|---| ----- | -------- | --------------------- |
9-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1_Two_Sum.py) | 1. hash O(n) and O(n) space<br>2. Sort and search with two points O(n) and O(1) space|
9+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1_Two_Sum.py) | 1. Hash O(n) and O(n) space<br>2. Sort and search with two points O(n) and O(1) space|
1010
| 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 |
1111
| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/5_Longest_Palindromic_Substring.py) | [Background knowledge](http://en.wikipedia.org/wiki/Longest_palindromic_substring)<br>1. DP if s[i]==s[j] and P[i+1, j-1] then P[i,j]<br>2. A palindrome can be expanded from its center<br>3. Manacher algorithm|
12+
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/7_Reverse_Integer.py) | Overflow when the result is greater than 2147483647
1213
| 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 |
1314
| 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) |
1415
| 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|
1516
| 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 |
1617
| 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 |
1718
| 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 |
19+
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/66_Plus_One.py) | Check if current digit == 9. |
1820
| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/72_Edit_Distance.py) | [Background](https://en.wikipedia.org/wiki/Edit_distance)<br> 1. DP O(n^2) space<br>2. DP O(n) space|
1921
| 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) |
2022
| 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|
@@ -23,4 +25,5 @@
2325
| 159 | [Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/159_Longest_Substring_with_At_Most_Two_Distinct_Characters.py) | Maintain a sliding window that always satisfies such condition |
2426
| 161 | [One Edit Distance](https://leetcode.com/problems/one-edit-distance/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/161_One_Edit_Distance.py) | 1. Check the different position and conditions<br>2. [Edit distance](https://leetcode.com/problems/edit-distance/)|
2527
| 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/163_Missing_Ranges.py) | Add lower -1 for special case, then check if curr - prev >= 2|
28+
| 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/186_Reverse_Words_in_a_String_II.py) | Reverse all and reverse each words |
2629
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def reverseWords(self, s):
3+
"""
4+
:type s: a list of 1 length strings (List[str])
5+
:rtype: nothing
6+
"""
7+
ls, pos = len(s), 0
8+
if s is None or ls == 0:
9+
return
10+
self.reverse(s, 0, ls)
11+
for i in range(ls + 1):
12+
if i == ls or s[i] == ' ':
13+
self.reverse(s, pos, i)
14+
pos = i + 1
15+
16+
def reverse(self, array_s, begin, end):
17+
for i in range((end - begin) / 2):
18+
array_s[begin + i], array_s[end - i - 1] = array_s[end - i - 1], array_s[begin + i]

python/7_Reverse_Integer.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
class Solution:
22
# @return an integer
33

4+
# def reverse(self, x):
5+
# max_int = 2147483647
6+
# if x == 0:
7+
# return 0
8+
# isPos = True
9+
# if x < 0:
10+
# x *= (-1)
11+
# isPos = False
12+
# ltemp = []
13+
# while x != 0:
14+
# temp = x % 10
15+
# ltemp.append(temp)
16+
# x /= 10
17+
# result = 0
18+
# # the main solution
19+
# for t in ltemp:
20+
# result = result * 10 + t
21+
# if result > max_int:
22+
# result = 0
23+
# if isPos:
24+
# return result
25+
# else:
26+
# return -1 * result
27+
428
def reverse(self, x):
5-
max_int = 2147483647
6-
if x == 0:
7-
return 0
8-
isPos = True
29+
# Note that in Python -1 / 10 = -1
30+
res, isPos = 0, 1
931
if x < 0:
10-
x *= (-1)
11-
isPos = False
12-
ltemp = []
32+
isPos = -1
33+
x = -1 * x
1334
while x != 0:
14-
temp = x % 10
15-
ltemp.append(temp)
35+
res = res * 10 + x % 10
36+
if res > 2147483647:
37+
return 0
1638
x /= 10
17-
result = 0
18-
for t in ltemp:
19-
result = result * 10 + t
20-
if result > max_int:
21-
result = 0
22-
23-
if isPos:
24-
return result
25-
else:
26-
return -1 * result
39+
return res * isPos

0 commit comments

Comments
 (0)