[Leetcode] [Java] Integer to Roman

Source: Internet
Author: User

Topic:

Given an integer, convert it to a Roman numeral.

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


Test instructions

Given an integer, convert it to Roman numerals.

The topic given input is guaranteed to be between 1-3999.


Algorithm Analysis:

Refer to the blog http://www.cnblogs.com/springfor/p/3886459.html


* For the old, the rule of not being familiar with Roman numerals is a problem. is actually a few symbols, then the law is 1,4,5,9,10 and then 10 times times each cycle.
* algorithm is to give a number, greater than 1000 when the write on a m, also more than 1000, and then write a M ... Finally less than 1000, see more than 900 not? , greater than 500 not ... So the analogy.
* Roman numerals are represented in the following form:
I 1
Y Z
X 10
L 50
C 100
D 500
M 1,000

AC Code:

public class Solution {public    String inttoroman (int num)     {        string str = "";            String [] symbol = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};            int [] value = {1000,900,500,400, +, 9,  5,  4,   1};           for (int. i=0;num!=0;i++)        {              while (num >= value[i])            {                  num-= value[i];                  str + = Symbol[i];}          }          return str;      }}


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode][java] 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.