[Leetcode] 12. Integer to Roman

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.