[C++] fast, easy to read / understand implementation


  • 2
    4
    class Solution {
    public:
        string intToRoman( int n )
        	{
            	    string s = "";
    
    		        for( ; n / 1000 ; ) 	{ s += "M"; 	n -= 1000; }
    		        if( n >= 900 ) 		    { s += "CM"; 	n -=  900; }
    		        if ( n >= 500 )	        { s += "D"; 	n -=  500; }
    		        if ( n >= 400 )		    { s += "CD";	n -=  400; }
    		        for( ; n / 100 ; ) 	    { s += "C"; 	n -=  100; }
    		        if( n >= 90 )		    { s += "XC"; 	n -=   90; }
    		        if ( n >= 50 )		    { s += "L"; 	n -=   50; }
    		        if ( n >= 40 )		    { s += "XL"; 	n -=   40; }
    		        for( ; n / 10 ; )       { s += "X"; 	n -=   10; }
        		    if ( n == 9 )		    { s += "IX";    return s;  }
    		        if ( n >= 5 )	    	{ s += "V";	    n -=    5; }
    		        if ( n == 4 )		    { s += "IV";    return s;  }
    		        for( ; n / 1 ; ) 		{ s += "I"; 	n -=    1; }
    		        return s;
            }
    };

Log in to reply
 

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