No.012: Integer to Roman, no.012roman

Source: Internet
Author: User

No.012: Integer to Roman, no.012roman

Question:

 

Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.

Official difficulty:

Medium

Translation:

A given integer is translated into a Roman number. The input Integer Range is 1-3999.

Additional information:

Roman numerals:
1. there are 7 roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000 ). There is no "0" in the Roman numerals ".
2. Repeated times: a roman number can be repeated three times at most.
3. Add right minus:
On the right side of a large number, remember a small number to indicate a large number and a small number.
On the left side of a large roman number, a small roman number is displayed, indicating that a large number reduces the number.
4. There is a limit on the number to be subtracted from the left, which is limited to I, X, and C. Only one number can be used on the left of a large number.
(*) The small numbers on the left of V and X can only use I.
(*) The small numbers on the left of L and C can only use X.
(*) The small numbers on the left side of D and M can only use C.

Ideas:

1. determine the maximum number of digits. Create a two-dimensional array corresponding to the number of digits.

2. Match the corresponding position of the two-dimensional array based on the number on each digit of the given input, and add the result string to StringBuffer word by word.

3. perform special processing on the 4 and 9 nodes first.

4. If it is not the case of 4 and 9, determine whether the value is greater than or equal to 5. If so, add V (assuming that the number of digits is being processed) to the result string.

5. Divide the current number by the remainder of 5 and accumulate the number of I.

Possible difficulties in solving problems:

1. When determining the maximum bit, you need to create a copy of the input value.

2. without considering the space consumption angle, you can define a 10 * n two-dimensional array instead of a 2 * n two-dimensional array, which is not prone to errors.

Solution code:

1 private static String method (int number) {2 // input parameter protection 3 if (number> 3999 | number <1) {4 return null; 5} 6 // result set 7 StringBuffer result = new StringBuffer (); 8 // Rome character set 9 char [] [] romanArray = new char [] [] {'I', 'V'}, {'x', 'L '}, {'C', 'D'}, {'M'}; 10 // create a copy to determine the maximum number of digits 11 int maxLevel = 0; 12 int copyNumber = number; 13 while (copyNumber> 0) {14 copyNumber/= 10; 15 maxLevel ++; 16} 17 while (-- maxLevel> = 0) {18 // The 19 int currentNumber = (int) (number/Math. pow (10, maxLevel) % 10); 20 // 4, 9 specially handles 21 if (currentNumber = 4) {22 result. append ("" + romanArray [maxLevel] [0] + romanArray [maxLevel] [1]); 23} else if (currentNumber = 9) {24 result. append ("" + romanArray [maxLevel] [0] + romanArray [maxLevel + 1] [0]); 25} else {26 // greater than or equal to 5 Processing 27 if (currentNumber/5 = 1) {28 result. append (romanArray [maxLevel] [1]); 29} 30 // plus 131 for (int I = 0; I <currentNumber % 5; I ++) {32 result. append (romanArray [maxLevel] [0]); 33} 34} 35} 36 return result. toString (); 37}View Code

Test Code address:

Https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/medium/Q012.java

LeetCode address:

Https://leetcode.com/problems/integer-to-roman/

PS: If you have any incorrect or more efficient methods, please leave a message. Thank you!

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.