Skip to content

leetcode problem 13 solution #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 013_Integer_To_Roman.py
  • Loading branch information
backendbuilderdev authored Jan 11, 2023
commit f7526739889f883071a99df94664d2757706b9d0
31 changes: 31 additions & 0 deletions 013_Integer_To_Roman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution:
# @param {integer} num
# @return {string}
def intToRoman(self, num):

res = ""

res+=self.helper(num/1000,"","","","M")
num%=1000
res+=self.helper(num/100,"CM","D","CD","C")
num%=100
res+=self.helper(num/10,"XC","L","XL","X")
num%=10
res+=self.helper(num,"IX","V","IV","I")
return res


def helper(self,num, nine,five,four,one):
res = ""
if num==9:
res+=nine
else:
if num>=5:
res+=five
num-=5
if num==4:
res+=four
else:
res+=one*num

return res