首页
登录 | 注册

13. Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

给定一个罗马数字,将其转换为整数。
保证输入在1到3999之间。

程序:

  public class RomanToIntSolution
    {
        //O(n) solution
        public static int RomanToInt(string s)
        {
            var charArray = s.ToCharArray();
            var sum = 0;
            for (int i = 0; i < charArray.Length; i++)
            {
                if (charArray[i] == 'M')
                    sum += 1000;
                else if (charArray[i] == 'D')
                    sum += 500;
                else if (charArray[i] == 'C')
                {
                    if (i < charArray.Length - 1 && charArray[i + 1] == 'M')
                    {
                        sum += 900;
                        i++;
                    }
                    else if (i < charArray.Length - 1 && charArray[i + 1] == 'D')
                    {
                        sum += 400;
                        i++;
                    }
                    else sum += 100;
                }
                else if (charArray[i] == 'L')
                    sum += 50;
                else if (charArray[i] == 'X')
                {
                    if (i < charArray.Length - 1 && charArray[i + 1] == 'C')
                    {
                        sum += 90;
                        i++;
                    }
                    else if (i < charArray.Length - 1 && charArray[i + 1] == 'L')
                    {
                        sum += 40;
                        i++;
                    }
                    else sum += 10;
                }
                else if (charArray[i] == 'V')
                    sum += 5;
                else if (charArray[i] == 'I')
                {
                    if (i < charArray.Length - 1 && charArray[i + 1] == 'X')
                    {
                        sum += 9;
                        i++;
                    }
                    else if (i < charArray.Length - 1 && charArray[i + 1] == 'V')
                    {
                        sum += 4;
                        i++;
                    }
                    else sum += 1;
                }
            }
            return sum;
        }
    }

单元测试(通过):

  [TestClass]
    public class UnitTest13_RomanToInteger
    {
        [TestMethod]
        public void TestMethodRomanToInt()
        {
            var list = new Dictionary<int, string>();
            list[1] = "I";
            list[2] = "II";
            list[3] = "III";
            list[4] = "IV";
            list[5] = "V";
            list[6] = "VI";
            list[7] = "VII";
            list[8] = "VIII";
            list[9] = "IX";
            list[10] = "X";
            list[14] = "XIV";
            list[19] = "XIX";
            list[20] = "XX";
            list[49] = "XLIX";
            list[50] = "L";
            list[99] = "XCIX";
            list[100] = "C";
            list[499] = "CDXCIX";
            list[500] = "D";
            list[999] = "CMXCIX";
            list[1000] = "M";
            list[3999] = "MMMCMXCIX";
            foreach (var pair in list)
                Assert.AreEqual(pair.Key, RomanToIntSolution.RomanToInt(pair.Value));
        }
    }


2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号