Skip to content

Commit d1198d1

Browse files
committed
12_Integer_to_Roman
13_Roman_to_Integer 54_Spiral_Matrix 137_Single_Number_II
1 parent d0ded7c commit d1198d1

File tree

4 files changed

+100
-38
lines changed

4 files changed

+100
-38
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
| 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|
1313
| 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
1414
| 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 |
15-
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/9_Palindrome_Number.py)| Get the len and check left and right with 10^len, 10|
15+
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/9_Palindrome_Number.py)| Get the len and check left and right with 10^len, 10 |
16+
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/12_Integer_to_Roman.py) | [Background knowledge](http://www.rapidtables.com/convert/number/how-number-to-roman-numerals.htm) Just like 10-digit number, divide and minus |
17+
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/13_Roman_to_Integer.py) | Add all curr, if curr > prev, then need to subtract 2 * prev |
1618
| 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) |
1719
| 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|
1820
| 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 |
1921
| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/21_Merge_Two_Sorted_Lists.py) | Add a dummy head, then merge two sorted list in O(m+n) |
2022
| 23 | [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/23_Merge_k_Sorted_Lists.py) | 1. Priority queue O(nk log k)<br>2. Binary merge O(nk log k)| |
2123
| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/24_Swap_Nodes_in_Pairs.py) | Add a dummy and store the prev |
2224
| 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 |
25+
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/54_Spiral_Matrix.py) | O(nm) 1. Turn in the corner and loop<br>2. (0, 1) -> (1, 0) -> (0, -1) -> (-1, 0) |
2326
| 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 |
2427
| 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. |
2528
| 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 |
@@ -31,7 +34,8 @@
3134
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/111_Minimum_Depth_of_Binary_Tree.py) | 1. Recursion, note that when size of left (ld) or right (rd) is 0, then min = 1 + ld + rd<br> 2. BFS check by level (right most), which is much faster than recursion |
3235
| 124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/124. Binary Tree Maximum Path Sum.py) | Recursion O(n) and O(n), max (left + node, right + node, left + node + right) |
3336
| 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) |
34-
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/136_Single_Number.py) | 1. Hash or set, O(n) and O(n)<br>2. xor O(n) and O(1) |
37+
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/136_Single_Number.py) | 1. Hash or set, O(n) and O(n)<br>2. xor O(n) and O(1) |
38+
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/137_Single_Number_II.py) | 1. ctypes 32 % 3 and &, O(n) and O(1)<br>2. ones, twos, threes as bitmask (e.g. ones represents ith bit had appeared once), O(n) and O(1) |
3539
| 138 | [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/138_Copy_List_with_Random_Pointer.py) | 1. Hash O(n) and O(n)<br>2. Modify original structure: Original->Copy->Original, then node.next.random = node.random.next, O(n) and O(1)|
3640
| 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 |
3741
| 156 | [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/156_Binary_Tree_Upside_Down.py) | p.left = parent.right, parent.right = p.right, p.right = parent, parent = p.left, p = left |

python/12_Integer_to_Roman.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,40 @@
66
# """
77
#
88
class Solution(object):
9+
# def intToRoman(self, num):
10+
# #http://www.rapidtables.com/convert/number/how-number-to-roman-numerals.htm
11+
# roman_dim = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'],
12+
# [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'],
13+
# [9,'IX'], [5, 'V'], [4, 'IV'], [1, 'I']]
14+
# if num == 0:
15+
# return ''
16+
# roman_str = ''
17+
# current, dim = num, 0
18+
# while current != 0:
19+
# while current // roman_dim[dim][0] == 0:
20+
# dim += 1
21+
# while current - roman_dim[dim][0] >= 0:
22+
# current -= roman_dim[dim][0]
23+
# roman_str += roman_dim[dim][1]
24+
# return roman_str
25+
926
def intToRoman(self, num):
10-
#http://www.rapidtables.com/convert/number/how-number-to-roman-numerals.htm
11-
roman_dim = [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'],
12-
[100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'],
13-
[9,'IX'], [5, 'V'], [4, 'IV'], [1, 'I']]
14-
if num == 0:
15-
return ''
16-
roman_str = ''
17-
current, dim = num, 0
18-
while current != 0:
19-
while current // roman_dim[dim][0] == 0:
20-
dim += 1
21-
while current - roman_dim[dim][0] >= 0:
22-
current -= roman_dim[dim][0]
23-
roman_str += roman_dim[dim][1]
24-
return roman_str
27+
values = [1000, 900, 500, 400,
28+
100, 90, 50, 40,
29+
10, 9, 5, 4, 1]
30+
symbols = ["M", "CM", "D", "CD",
31+
"C", "XC", "L", "XL",
32+
"X", "IX", "V", "IV",
33+
"I"]
34+
roman = ''
35+
i = 0
36+
while num > 0:
37+
k = num / values[i]
38+
for j in range(k):
39+
roman += symbols[i]
40+
num -= values[i]
41+
i += 1
42+
return roman
2543

2644
if __name__ == '__main__':
2745
# begin

python/137_Single_Number_II.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
# def singleNumber(self, nums):
3+
# """
4+
# :type nums: List[int]
5+
# :rtype: int
6+
# """
7+
# import ctypes
8+
# # note that if res is not c 32
9+
# # there will be errors
10+
# count = [0] * 32
11+
# res = ctypes.c_int32(0)
12+
# for i in range(32):
13+
# for num in nums:
14+
# if (ctypes.c_int32(num).value >> i) & 1:
15+
# count[i] += 1
16+
# res.value |= ((count[i] % 3) << i)
17+
# return res.value
18+
19+
def singleNumber(self, nums):
20+
# bitmask
21+
# ones as a bitmask to represent the ith bit had appeared once.
22+
# twos as a bitmask to represent the ith bit had appeared twice.
23+
# threes as a bitmask to represent the ith bit had appeared three times.
24+
ones, twos, threes = 0, 0, 0
25+
for num in nums:
26+
twos |= ones & num
27+
ones ^= num
28+
threes = ones & twos
29+
ones &= ~threes
30+
twos &= ~threes
31+
return ones

python/13_Roman_to_Integer.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
# class Solution(object):
2-
# def romanToInt(self, s):
3-
# """
4-
# :type s: str
5-
# :rtype: int
6-
# """
7-
81
class Solution:
9-
# @return an integer
2+
# def romanToInt(self, s):
3+
# """
4+
# :type s: str
5+
# :rtype: int
6+
# """
7+
# roman = {'I': 1, 'V': 5, 'X': 10,
8+
# 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
9+
# result = 0
10+
# last = s[-1]
11+
# for t in reversed(s):
12+
# if t == 'C' and last in ['D', 'M']:
13+
# result -= roman[t]
14+
# elif t == 'X' and last in ['L', 'C']:
15+
# result -= roman[t]
16+
# elif t == 'I' and last in ['V', 'X']:
17+
# result -= roman[t]
18+
# else:
19+
# result += roman[t]
20+
# last = t
21+
# return result
22+
1023
def romanToInt(self, s):
1124
roman = {'I': 1, 'V': 5, 'X': 10,
1225
'L': 50, 'C': 100, 'D': 500, 'M': 1000}
13-
result = 0
14-
last = s[-1]
15-
for t in reversed(s):
16-
if t == 'C' and last in ['D', 'M']:
17-
result -= roman[t]
18-
elif t == 'X' and last in ['L', 'C']:
19-
result -= roman[t]
20-
elif t == 'I' and last in ['V', 'X']:
21-
result -= roman[t]
22-
else:
23-
result += roman[t]
24-
last = t
25-
return result
26+
prev, total = 0, 0
27+
for c in s:
28+
curr = roman[c]
29+
total += curr
30+
# need to subtract
31+
if curr > prev:
32+
total -= 2 * prev
33+
prev = curr
34+
return total
2635

0 commit comments

Comments
 (0)