[Leetcode] [12] Integer to Roman implementation of-java for parsing int to Roman character time complexity as constant

Source: Internet
Author: User

Q:

Given an integer, convert it to a Roman numeral.

Input is guaranteed to being within the range from 1 to 3999.

A:

I really do not know what is Roman, I went to check the Roman font, this topic meaning is also very simple that I want to enter a number can be guaranteed to be 1~3999 but you want to convert this Arabic numeral into Roman numerals.

First we need to know how the Roman numerals are expressed, and the Roman numerals are as follows:

There are seven Roman numerals, namely I (1), V (5), X (Ten), L (+), C (+), D (+), M (1000).
Also roman numerals have the following rules:
It's all copy. Let's look at this topic carefully, just enter the Arabic numeral a = XI + yv+zx ... The values of x, Y, Z, etc. are then matched according to rules. We certainly will not solve this equation, but to be sure, he is like the Arabic numeral, the first end must be 1~9 middle. Add down is 10, hundreds, thousands of conversions added. We probably need the following tables: 1~9 (9) 10~90 (9) 100~900 (9) 1000~3000 (3) Why do we choose to compare, because so fast ah, but think about it, there is not a formula directly generated? All the 1 (10, 100, 1000) superimposed 4~8 are 5 (50, 500) plus 1 (10, 100) and 9 (10, 100) minus 1000 (1, 10) are obtained. We can also use this rule to calculate the values that need to be converted. So we only need to start from the tail to traverse this int, the specific traversal method please look at my previous copy of the source code to imitate the implementation of the JDK click to open the link to the following details:
public class Integertoroman {public static void main (string[] args) {int a = 999; System.out.println (Method (a));} private static String method (int i) {//TODO auto-generated method Stubint r =0;int q = 1;int times = 0;  String Roman = ""; while (true) {times ++;q = (i * 52429) >>> 19;//to tail 1-bit,2<< (16+3) =524288 52429/524288 approx. 0.1r = I-((Q << 3) + (q << 1));//r=i-q*10, truncate the Last I=q;switch (times) {case 1://digit Roman = Convertroman (R, "I", "V", "X" ); Break;case 2://10-bit Roman = Convertroman (R, "X", "L", "C") +roman;break;case 3://hundred Roman = Convertroman (R, "C", "D", "M") +ro Man;break;case 4://thousand Roman = Convertroman (R, "M", "null", "null") +roman;break;} if (q==0) break;} return Roman;}  /** * A Roman digital conversion * @param R need to convert roman numerals * @param one represents 1 of the Roman alphabet * @param five this one represents 5 of the Roman alphabet * @param ten this one represents 10 of the Roman alphabet * @return */public static string Convertroman (int r,string one,string five,string ten) {String Roman = ""; if (r==0) return ""; else if (0 <r&&r<=3) {//onestring s = ""; for (int k = 0;k<r;k++) {S+=one;} Roman = Roman+s;} else if (3<r&&r<=8) {//five+/-onestring s = ""; if (r==4) {s=one+five;} else {for (int k = 5;k<r;k++) {s+=one;} S= Five+s;} Roman = Roman+s;} else if (r = = 9) {//ten-oneroman = One+ten;} return Roman;}
Perfect~ Perfect Solution

[Leetcode] [12] Integer to Roman implementation of-java for parsing int to Roman character time complexity as constant

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.