Int to Roman c# solution


  • 0
    P
        private string IntToRoman(int num)
        {
            string output = String.Empty;
    
            var lookUpTable = new List<char[]>
            {
                new [] {'I', 'V', 'X'},
                new [] {'X', 'L', 'C'},
                new [] {'C', 'D', 'M'},
                new [] {'M', 'K', 'K'}
            };
    
            
            for (int weight = 0; num > 0; weight++)
            {
                int rem = num % 10;
                output = CalcRomanDigit(rem, lookUpTable[weight]) + output;
                num = num / 10;
            }
    
            return output;
        }
    
        private string CalcRomanDigit(int num, char[] arr)
        {
            if (num == 1) return arr[0].ToString();
            
            if (num == 2) return $"{arr[0]}{arr[0]}";
    
            if(num == 3) return $"{arr[0]}{arr[0]}{arr[0]}";
    
            if (num == 4) return $"{arr[0]}{arr[1]}";
    
            if (num == 5) return $"{arr[1]}";
    
            if (num == 6)  return $"{arr[1]}{arr[0]}";
    
            if (num == 7) return $"{arr[1]}{arr[0]}{arr[0]}";
    
            if (num == 8) return $"{arr[1]}{arr[0]}{arr[0]}{arr[0]}";
    
            if (num == 9) return $"{arr[0]}{arr[2]}";
            return string.Empty;
    
        }

Log in to reply
 

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