LeetCode 12
Integer to Roman
Problem Description:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.Solution:
主要是从一些特例找出一般规律,从千位、百位、十位、个位出发进行考虑(很惭愧,此解法是从leetcode-discuss学(抄)来的)
class Solution {
public:
string intToRoman(int num) {
vector<string> Thousands = {"", "M", "MM", "MMM"};
vector<string> Hundreds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
vector<string> Tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
vector<string> Digits = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
string result = Thousands[num/1000]+Hundreds[(num%1000)/100]+Tens[(num%100)/10]+Digits[num%10];
return result;
}
};