About Roman Numerals:
I:1
V:5
X:10
L:50
c:100
d:500
m:1000
The letters can be repeated, but not more than three times, when more than three times are required, expressed in combination with the next bit:
I:1, Ii:2, Iii:3, Iv:4
c:100, cc:200, ccc:300, cd:400
Extract each bit of digit, then convert to Roman numerals
Public classSolution {Private Static Char[] chars = {{' I ', ' V '}, {' X ', ' L '}, {' C ', ' D '}, {' M ', ' O '}}; PublicString Inttoroman (intnum) { intCopy_num =num; intCount = 0; StringBuilder result=NewStringBuilder (); while(Copy_num! = 0) { intx = COPY_NUM/10; intdigit = Copy_num-x * 10; Switch(digit) { Case3:result.append (chars[count][0]); Case2:result.append (chars[count][0]); Case1:result.append (chars[count][0]); Case0: Break; Case4:result.append (chars[count][1]); Result.append (Chars[count][0]); Break; Case8:result.append (chars[count][0]); Case7:result.append (chars[count][0]); Case6:result.append (chars[count][0]); Case5:result.append (chars[count][1]); Break; Case9:result.append (Chars[count + 1][0]); Result.append (Chars[count][0]); Break; } Count++; Copy_num=x; } result=Result.reverse (); returnresult.tostring (); }}
[Leetcode] 12. Integer to Roman