Leetcode notes: Integer to Roman

Source: Internet
Author: User

I. Title Description

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

Two. Topic analysis

Roman Numerals Summary:

1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

10~90: {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

100~900: {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

1000~3000: {"M", "MM", "MMM"}.

Reference to the Roman numeral: http://baike.baidu.com/view/1246899.htm

The problem is to enter an Arabic numeral and convert it to a Roman numeral string.

The problem can be manipulated using the simplest operation, which takes the low to high position of the number. Take one bit at a time, take one and then the number is 10, and the number taken out is expressed in Roman numerals. It is important to note that if the current bit is not the original number, you need to expand 10 times times or 100 times times.

Three. Sample code

Class Solution { Public:string Inttoroman(intNUM) {stringStrstringsymbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};int value[]=    { +, the, -, -, -, -, -, +,Ten,9,5,4,1}; for(intI=0; num!=0; ++i) { while(num>=value[i]) {num-=value[i];            Str+=symbol[i]; }        }returnStr }};

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode notes: 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.