class Solution { public: string intToRoman(int num) { string result; int count = num / 1000; for(int i = 0 ; i < count; i++) { result += "M"; } num = num % 1000; count = num / 100; appendTo(count, "C","D", "M", result); num = num % 100; count = num / 10; appendTo(count, "X","L","C", result); num = num % 10; count = num; appendTo(count, "I","V","X", result); return result; } void appendTo(int count, const std::string& smaller, const std::string& larger, const std::strin& evenLarger, std::string& result) { if(count <; 4) { for(int i = 0; i < count; i++) { result+= smaller; } } else if (count == 4) { result += smaller + larger; } else if(count <; 9) { result += larger; for(int i = 5; i < count; i++) { result += smaller; } } else if(count == 9) { result += smaller + evenLarger; } } };
3/30/2014
Leetcode -- Integer to Roman
Solution 03/30/2014:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment