simple to understand C++ ,O(n),42ms


  • 0
    Z
    int romanToInt(string s) {
    		string str[4][9] = {
    			{ "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
    			{ "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
    			{ "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
    			{ "M", "MM", "MMM" }
    		};
    		int x[4] = { 1, 10, 100, 1000 };
    		int result = 0;
    		int ptr = 0;
    		for (int i = 3; i >= 0; i--)
    		{
    			int j = i==3?2:8;
    			while (str[i][j]!=""&&j>=0)
    			{
    				if (s.find(str[i][j])==ptr)
    				{
    					result  += x[i]*(j+1);
    					ptr += str[i][j].length();
    					break;
    				}
    				j--;
    			}
    			
    		}
    		return result;
    	}
    

Log in to reply
 

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