c++ Easy Understand


  • 0
    Y

    class Solution {
    public:
    string intToRoman(int num) {
    string result = "";
    if (num <= 0 || num >= 4000) return result;

    	vector<string> v;
    	v.resize(1001);
    	v[1]	= "I";	v[5]	= "V";	v[10]	= "X";	v[50] = "L";
    	v[100]	= "C";	v[500]	= "D";	v[1000]	= "M";
    
    	int i = 0;
    	string tmpresult = "";
    	while (num != 0)
    	{
    		int tmp = num % 10;
    		
    		switch (tmp)
    		{
    		case 1: tmpresult = v[1 * pow(10, i)]; break;
    		case 2: tmpresult = v[1 * pow(10, i)] + v[1 * pow(10, i)]; break;
    		case 3: tmpresult = v[1 * pow(10, i)] + v[1 * pow(10, i)] + v[1 * pow(10, i)]; break;
    		case 4: tmpresult = v[1 * pow(10, i)] + v[5 * pow(10, i)]; break;
    		case 5: tmpresult = v[5 * pow(10, i)]; break;
    		case 6: tmpresult = v[5 * pow(10, i)] + v[1 * pow(10, i)]; break;
    		case 7: tmpresult = v[5 * pow(10, i)] + v[1 * pow(10, i)] + v[1 * pow(10, i)]; break;
    		case 8: tmpresult = v[5 * pow(10, i)] + v[1 * pow(10, i)] + v[1 * pow(10, i)] + v[1 * pow(10, i)]; break;
    		case 9: tmpresult = v[1 * pow(10, i)] + v[10 * pow(10, i)]; break;
    		}
    		result = tmpresult + result;
    		tmpresult = "";
    		num = num / 10;
    		i++;
    	}
    
    	return result;
    }
    

    };


Log in to reply
 

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