Skip to content

Instantly share code, notes, and snippets.

@spurscho
Created December 26, 2019 10:46
Show Gist options
  • Save spurscho/2b718aafc171c8bb4056a2d01e3dddb9 to your computer and use it in GitHub Desktop.
Save spurscho/2b718aafc171c8bb4056a2d01e3dddb9 to your computer and use it in GitHub Desktop.
12. Integer to Roman
class Solution {
func intToRoman(_ num: Int) -> String {
// if statement is faseter than switch statement (-8ms)
switch num {
case 0 :
return ""
case 1000... :
return "M" + intToRoman(num - 1000)
case 900... :
return "CM" + intToRoman(num - 900)
case 500... :
return "D" + intToRoman(num - 500)
case 400... :
return "CD" + intToRoman(num - 400)
case 100... :
return "C" + intToRoman(num - 100)
case 90... :
return "XC" + intToRoman(num - 90)
case 50... :
return "L" + intToRoman(num - 50)
case 40... :
return "XL" + intToRoman(num - 40)
case 10... :
return "X" + intToRoman(num - 10)
case 9... :
return "IX" + intToRoman(num - 9)
case 5... :
return "V" + intToRoman(num - 5)
case 4... :
return "IV" + intToRoman(num - 4)
case 0... :
return "I" + intToRoman(num - 1)
default :
return ""
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment