Solution in C#


  • 0
    P
    static public string IntToRoman(int num)
            {
                string output = string.Empty;
    
                // dictionary of the int values and equivalent roman values
                Dictionary<int, char> precedenceValues = new Dictionary<int, char>();
                precedenceValues.Add(1, 'I');
                precedenceValues.Add(5, 'V');
                precedenceValues.Add(10, 'X');
                precedenceValues.Add(50, 'L');
                precedenceValues.Add(100, 'C');
                precedenceValues.Add(500, 'D');
                precedenceValues.Add(1000, 'M');
    
                // invalid input
                if (num >= 4000)
                {
                    return string.Empty;
                }
    
                // maximum size the input can have is upto 4 digits
                int[] number = new int[4];
                int i = 0;
                bool twoValues = false;
                string placeValueRoman = string.Empty;
    
                // save the each digit of the number in different index of array
                while (num > 0)
                {
                    number[i++] = num % 10;
                    num = num / 10;
                }
    
                // starting from the maximum index value of the array
                for (i = 3; i >= 0; i--)
                {
                    // set place value in roman as empty
                    placeValueRoman = string.Empty;
    
                    if (number[i] > 0)
                    {
                        // to check if it is 4 or 9; make it 5 or 10 and then subtract 1 from it.
                        if (precedenceValues.ContainsKey(number[i] + 1))
                        {
                            twoValues = true;
                            number[i] += 1;
                        }
    
                        int val = (int)(number[i] * Math.Pow(10, i));
    
                        while (val > 0)
                        {
                            int key = precedenceValues.Where(a => a.Key <= val).Max(a => a.Key);
                            char valueRoman = precedenceValues[key];
                            val -= key;
                            placeValueRoman = placeValueRoman + valueRoman.ToString();
                        }
    
                        // prefix the roman number with the smaller value
                        if (twoValues)
                        {
                            char valueRoman = precedenceValues[(int)Math.Pow(10, i)];
                            placeValueRoman = valueRoman.ToString() + placeValueRoman;
                            twoValues = false;
                        }
    
                        output = output + placeValueRoman;
                    }
                }
    
                return output;
            }
    

Log in to reply
 

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