Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 630058d

Browse files
committed
Update to 016
Update to 016
1 parent 310929e commit 630058d

4 files changed

+55
-4
lines changed

Python3/004_Median_of_Two_Sorted_Arrays.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!usr/bin/env python
22
# -*- coding:utf-8 -*-
3-
4-
53
'''
64
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
75
'''
86

7+
98
class Solution(object):
109
def findMedianSortedArrays(self, nums1, nums2):
1110
"""

Python3/009_Palindrome_Number.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!usr/bin/env python
22
# -*- coding:utf-8 -*-
3-
4-
53
'''
64
Determine whether an integer is a palindrome. Do this without extra space.
75
@@ -14,6 +12,7 @@
1412
There is a more generic way of solving this problem.
1513
'''
1614

15+
1716
class Solution(object):
1817
def isPalindrome(self, x):
1918
"""

Python3/012_Integer_to_Roman.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
#!usr/bin/env python
22
# -*- coding:utf-8 -*-
3+
'''
4+
Given an integer, convert it to a roman numeral.
35
6+
Input is guaranteed to be within the range from 1 to 3999.
7+
'''
48

59

10+
class Solution(object):
11+
def intToRoman(self, num):
12+
"""
13+
:type num: int
14+
:rtype: str
15+
"""
16+
nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
17+
strings = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
18+
result = ''
19+
for i in range(len(nums)):
20+
while num >= nums[i]:
21+
num -= nums[i]
22+
result += strings[i]
23+
return result
24+
25+
26+
# Test cases
27+
if __name__ == "__main__":
28+
assert Solution().intToRoman(12) == "XII"
29+
assert Solution().intToRoman(21) == "XXI"
30+
assert Solution().intToRoman(99) == "XCIX"
631

732

Python3/013_Roman_to_Integer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
#!usr/bin/env python
22
# -*- coding:utf-8 -*-
3+
'''
4+
Given a roman numeral, convert it to an integer.
35
6+
Input is guaranteed to be within the range from 1 to 3999.
7+
'''
8+
9+
10+
class Solution(object):
11+
def romanToInt(self, s):
12+
"""
13+
:type s: str
14+
:rtype: int
15+
"""
16+
map = {"M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1}
17+
result = 0
18+
for i in range(len(s)):
19+
if i > 0 and map[s[i]] > map[s[i-1]]:
20+
result -= map[s[i-1]]
21+
result += map[s[i]] - map[s[i-1]]
22+
else:
23+
result += map[s[i]]
24+
return result
25+
26+
27+
# Test cases
28+
if __name__ == "__main__":
29+
assert Solution().romanToInt("XII") == 12
30+
assert Solution().romanToInt("XXI") == 21
31+
assert Solution().romanToInt("LVIII") == 58
432

533

634

0 commit comments

Comments
 (0)