Skip to content

Commit 427cdd7

Browse files
Merge pull request #2 from Pal-Sandeep/Integer_To_Roman
leetcode problem 13 solution
2 parents db1650b + f752673 commit 427cdd7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

013_Integer_To_Roman.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
# @param {integer} num
3+
# @return {string}
4+
def intToRoman(self, num):
5+
6+
res = ""
7+
8+
res+=self.helper(num/1000,"","","","M")
9+
num%=1000
10+
res+=self.helper(num/100,"CM","D","CD","C")
11+
num%=100
12+
res+=self.helper(num/10,"XC","L","XL","X")
13+
num%=10
14+
res+=self.helper(num,"IX","V","IV","I")
15+
return res
16+
17+
18+
def helper(self,num, nine,five,four,one):
19+
res = ""
20+
if num==9:
21+
res+=nine
22+
else:
23+
if num>=5:
24+
res+=five
25+
num-=5
26+
if num==4:
27+
res+=four
28+
else:
29+
res+=one*num
30+
31+
return res

0 commit comments

Comments
 (0)