[LeetCode-interview algorithm classic-Java implementation] [012-Integer to Roman (number to Rome character)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [012-Integer to Roman (number to Rome character)], leetcode -- java
[012-Integer to Roman )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

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

Theme

Enter a number and convert it into a Roman number. The entered number is in the range of [1, 3999.
The expression of the Roman numerals:
Example of single digit: (I, 1) (II, 2) (III, 3) (IV, 4) (V, 5) (VI, 6) (VII, 7) (VIII, 8) (IX, 9)
Ten-digit example: (X, 10) (XI, 11) (XII, 12) (XIII, 13) (XIV, 14) (XV, 15) (XVI, 16) (XVII, 17) (XVIII, 18) (XIX, 19) (XX, 20) (XXI, 21) (XXII, 22) (XXIX, 29) (XXX, 30) (XXXIV, 34) (XXXV, 35) (XXXIX, 39) (XL, 40) (L, 50) (LI, 51) (LV, 55) (LX, 60) (LXV, 65) (LXXX, 80) (XC, 90) (XCIII, 93) (XCV, 95) (XCVIII, 98) (XCIX, 99)
Example of hundreds of BITs: (C, 100) (CC, 200) (CCC, 300) (CD, 400) (D, 500) (DC, 600) (DCC, 700) (DCCC, 800) (CM, 900) (CMXCIX, 999)
Example of thousands of BITs: (M, 1000) (MC, 1100) (MCD, 1400) (MD, 1500) (MDC, 1600) (MDCLXVI, 1666) (MDCCCLXXXVIII, 1888) (MDCCCXCIX, 1899) (MCM, 1900) (MCMLXXVI, 1976) (MCMLXXXIV, 1984) (MCMXC, 1990) (MM, 2000) (MMMCMXCIX, 3999)

Solutions

Create a two-dimensional array to save the writing of 1-9 digits on each digit, perform the remainder evaluate operation on the digit, and calculate the character and value of each digit from the low position to the final result.

Code Implementation
Public class Solution {public String intToRoman (int num) {String [] [] base = new String [] [] {"I", "II", "III ", "IV", "V", "VI", "VII", "VIII", "IX"}, // represents {"X", "XX ", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, // represents {"C ", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM "}, // 100x representation {"M", "MM", "MMM ","","","","","",""}}; // The String result = "" in the thousands of BITs; // each time a number is exceeded, it indicates the current number of the processed bits (from small to large) // I record The number of digits for (int I = 0; num! = 0; num/= 10, I ++) {// if it is not 0, it indicates that there is a value on this digit. The addition operation if (num % 10! = 0) {// result = base [I] [num % 10-1] + result ;}} return result ;}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46963375]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.