Fast and Concise c++


  • 0
    P
    class Solution {
        vector<string> Rom = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        vector<int> Num = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    public:
        string intToRoman(int num) {
            string ans;
            for(int i=0;i<Rom.size();++i){
                if(num>=Num[i]){
                    for(int j=0;j<num/Num[i];++j) ans += Rom[i];
                    num -= (num/Num[i])*Num[i];
                }
            }
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.