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;
      }
    };

  • 相关阅读:
    chrome远程调试真机上的app
    高性能Cordova App开发学习笔记
    eclipse导入cordova项目
    跨域后模拟器上还是不能显示数据
    跨域请求数据
    eclipse导入cordova创建的项目
    cordova添加platform
    sdk更新代理设置
    NPM安装之后CMD中不能使用
    android开发环境搭建
  • 原文地址:https://www.cnblogs.com/devin-guwz/p/6497300.html
Copyright © 2011-2022 走看看