Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit630058d

Browse files
committed
Update to 016
Update to 016
1 parent310929e commit630058d

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
classSolution(object):
109
deffindMedianSortedArrays(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
classSolution(object):
1817
defisPalindrome(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+
classSolution(object):
11+
defintToRoman(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+
foriinrange(len(nums)):
20+
whilenum>=nums[i]:
21+
num-=nums[i]
22+
result+=strings[i]
23+
returnresult
24+
25+
26+
# Test cases
27+
if__name__=="__main__":
28+
assertSolution().intToRoman(12)=="XII"
29+
assertSolution().intToRoman(21)=="XXI"
30+
assertSolution().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+
classSolution(object):
11+
defromanToInt(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+
foriinrange(len(s)):
19+
ifi>0andmap[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+
returnresult
25+
26+
27+
# Test cases
28+
if__name__=="__main__":
29+
assertSolution().romanToInt("XII")==12
30+
assertSolution().romanToInt("XXI")==21
31+
assertSolution().romanToInt("LVIII")==58
432

533

634

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp