Leetcode12 ---------- Integer to Roman

Source: Internet
Author: User

Leetcode12 ---------- Integer to Roman

Given an integer, convert it to a roman numeral.

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

Rome numeric rules: Reference wiki: http://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97
1. There are 7 roman numerals, namely, I (1), V (5), X (10), L (50), C (100), and D (500) and M (1000 ).

There is no "0" in the Roman numerals ".
2. Repeated times: a roman number can be repeated up to three times.
3. add and subtract from the right:
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 be 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.

// LeetCode, Integer to Roman // time complexity O (num), space complexity O (1) class Solution {public: string intToRoman (int num) {const int radix [] = {1000,900,500,400,100, 90, 50, 40, 10, 9, 5, 4, 1}; const string symbol [] = {"M", "CM ", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV ", "I"}; string roman; for (size_t I = 0; num> 0; ++ I) {int count = num/radix [I]; num % = radix [I]; for (; count> 0; -- count) roman + = symbol [I] ;}return 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.