zoukankan      html  css  js  c++  java
  • 13. Roman to Integer(C++)

    Given a roman numeral, convert it to an integer.

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

    Solution:

    class Solution {
    public:
      int romanToInt(string s) {
        int len=s.size();
        int sum=0,i=0;
        while(i<len){
          switch(s[i]){
            case 'M': sum+=1000;break;
            case 'D': sum+=500;break;
            case 'C': if(s[i+1]=='M'||s[i+1]=='D'){
              sum-=100;
            }else{
              sum+=100;
            }
            break;
            case 'L': sum+=50; break;
            case 'X': if(s[i+1]=='L'||s[i+1]=='C'){
              sum-=10;
            }else{
              sum+=10;
            }
            break;
            case 'V': sum+=5;break;
            case 'I': if((i+1)!=len &&(s[i+1]=='V'||s[i+1]=='X')){
              sum-=1;
            }else{
              sum+=1;
            }
          }
          i++;
        }
        return sum;
      }
    };

  • 相关阅读:
    JS的运行机制
    Vue路由
    javascript的逼格
    Vue开发中遇到的问题及解决方案
    模块模式
    2019年终总结
    http知识总结
    小议函数的节流和防抖
    nodejs初探一二
    Object是个什么鬼
  • 原文地址:https://www.cnblogs.com/devin-guwz/p/6497300.html
Copyright © 2011-2022 走看看