zoukankan      html  css  js  c++  java
  • 罗马数字转换成整数

    方法一:

    class Solution {
    public:
        int romanToInt(string s) {
            
            int len=s.size();
            map<string,int> number{
                {"I",1},
                {"V",5},
                {"X",10},
                {"L",50},
                {"C",100},
                {"D",500},
                {"M",1000},
                {"IV",4},
                {"IX",9},
                {"XL",40},
                {"XC",90},
                {"CD",400},
                {"CM",900}
            };
            
            string s1;
            string s2;
            string s3;
            int sum=0;
            int i=0;
            for(i;i<=len-2;)
            {
                s1=s.substr(i,1);
                s2=s.substr(i+1,1);
                if(number.at(s1)<number.at(s2))
                {
                    s3=s1+s2;
                    sum=number.at(s3)+sum;
                    i=i+2;
                }
                else
                {
                    sum=number.at(s1)+sum;
                    i++;
                }
                
            }
            if(i==len-1)
            {
                sum=number.at(s.substr(len-1,1))+sum;
            }
            
            return sum;
        }
    };

  • 相关阅读:
    逐级汇总示例(循环逐级累计法).sql
    UdtSsn.cs
    2013届大华股份 软件算法类试题 D卷
    TCP与UDP区别
    浙江大华 研发类试题
    百度面试经历
    Contiki入门学习
    09网易校园招聘笔试题
    (转载)利用webkit抓取动态网页和链接
    2012.9.23 搜狗笔试
  • 原文地址:https://www.cnblogs.com/wzhtql/p/10217484.html
Copyright © 2011-2022 走看看