zoukankan      html  css  js  c++  java
  • LeetCode 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.

    罗马数字:可以查看维基百科的定义

    .

    最大为MMMCMXCIX,我们需要注意的是9的写法是IX,4的写法是IV

    这样我们遍历整个字符串后就可以得到结果,也可以将求值重新封装

    成一个新的函数,因为好多部分都是重复的,下面是代码

    class Solution {
    public:
        int romanToInt(string s) {
            int sum = 0;
            int cnt = 0;
            int i = 0;
                if (i + cnt<s.size() && s[i] == 'M')
                {
                    ++cnt;
                    for (;s[i + cnt] == 'M';++cnt);
                    sum += cnt * 1000;
                    i += cnt;
                    cnt = 0;
                }
                if (i + cnt < s.size() && s[i] == 'D')
                {
                    ++cnt;
                    for (;s[i + cnt] == 'C';++cnt);
                    sum += (4 + cnt) * 100;
                    i += cnt;
                    cnt = 0;    
                }
                if (i + cnt < s.size()&&s[i] == 'C')
                {
                    ++cnt;
                    for (;s[i + cnt] == 'C';++cnt);
                    if (1 == cnt)
                    {
                        if (s[i + cnt] != 'M'&&s[i + cnt] != 'D')
                        {
                            sum += 100;
                            i += cnt;
                            cnt = 0;
                        }
                        if (s[i + cnt] == 'M')
                        {
                            ++cnt;
                            sum += 900;
                            i += cnt;
                            cnt = 0;
                        }
                        if (s[i + cnt] == 'D')
                        {
                            ++cnt;
                            sum += 400;
                            i += cnt;
                            cnt = 0;
                        }
                    }
                    if (2 == cnt)
                    {
                        sum += 200;
                        i += cnt;
                        cnt = 0;
                    }
                    if (3 == cnt)
                    {
                        sum += 300;
                        i += cnt;
                        cnt = 0;
                    }    
                }
                if (i + cnt < s.size() && s[i] == 'L')
                {
                    ++cnt;
                    for (;s[i + cnt] == 'X';++cnt);
                    sum += (4 + cnt) * 10;
                    i += cnt;
                    cnt = 0;
                }
                if (i + cnt < s.size() && s[i] == 'X')
                {
                    ++cnt;
                    for (;s[i + cnt] == 'X';++cnt);
                    if (1 == cnt)
                    {
                        if (s[i + cnt] != 'L'&&s[i + cnt] != 'C')
                        {
                            sum += 10;
                            i += cnt;
                            cnt = 0;
                        }
                        if (s[i + cnt] == 'C')
                        {
                            ++cnt;
                            sum += 90;
                            i += cnt;
                            cnt = 0;
                        }
                        if (s[i + cnt] == 'L')
                        {
                            ++cnt;
                            sum += 40;
                            i += cnt;
                            cnt = 0;
                        }
                    }
                    if (2 == cnt)
                    {
                        sum += 20;
                        i += cnt;
                        cnt = 0;
                    }
                    if (3 == cnt)
                    {
                        sum += 30;
                        i += cnt;
                        cnt = 0;
                    }
                }
                    if (i + cnt < s.size() && s[i] == 'V')
                    {
                        ++cnt;
                        for (;s[i + cnt] == 'I';++cnt);
                        sum += (4 + cnt) * 1;
                        i += cnt;
                        cnt = 0;
                    }
                    if (i + cnt < s.size() && s[i] == 'I')
                    {
                        ++cnt;
                        for (;s[i + cnt] == 'I';++cnt);
                        if (1 == cnt)
                        {
                            if (s[i + cnt] != 'V'&&s[i + cnt] != 'X')
                            {
                                sum += 1;
                                i += cnt;
                                cnt = 0;
                            }
                            if (s[i + cnt] == 'X')
                            {
                                ++cnt;
                                sum += 9;
                                i += cnt;
                                cnt = 0;
                            }
                            if (s[i + cnt] == 'V')
                            {
                                ++cnt;
                                sum += 4;
                                i += cnt;
                                cnt = 0;
                            }
                        }
                        if (2 == cnt)
                        {
                            sum += 2;
                            i += cnt;
                            cnt = 0;
                        }
                        if (3 == cnt)
                        {
                            sum += 3;
                            i += cnt;
                            cnt = 0;
                        }
                    }
                return sum;
        }
    };
  • 相关阅读:
    VIM快捷键(转)
    VIM中文乱码
    vsftpd.conf 联机手册
    keepalived nginx 主备配置
    Keepalived 主备配置
    Linux centos7 安装 keepalived-2.0.6
    Linux centos开机执行JAR Shell脚本
    Nginx负载均衡案例
    Windows虚拟机安装Linux系统
    Linux centos7 redis安装教程
  • 原文地址:https://www.cnblogs.com/csudanli/p/5885666.html
Copyright © 2011-2022 走看看