zoukankan      html  css  js  c++  java
  • Roman to Integer

    class Solution {
    public:
        int romanToInt(string s) {
            int length = s.size();
            int result = 0;
            int pre =getnum (s[0]);
            for (int i = 1 ;i<length ;i++)
            {int current = getnum(s[i]);
            //if (pre*5==current||pre*10==current)从左到右,如果当前值比右边的值小,则减去当前值,否则加上当前的值
            if(pre<current)
            result = result-pre;
            else
            result = result+pre;
            pre = current; 
            }
            return result+pre ;
        }
      int getnum(char c)
      {//I(1)、V(5)、X(10)、L(50)、C(100)、D(500)、M(1000)
          if (c=='I')
           return 1;
          if (c=='V')
           return 5;
          if (c=='X')
           return 10;
          if (c=='L')
           return 50;
          if (c=='C')
           return 100;
          if (c=='D')
           return 500;
          if (c=='M')
           return 1000;
      }
    };

  • 相关阅读:
    Spring +quartz获取ApplicationContext上下文
    开源 java CMS
    js实现页面跳转的几种方式
    hdu-4089-Activation-概率dp
    linux 内核定时器
    linux 短延时
    linux 基于 jiffy 的超时
    linux 让出处理器
    linux 延后执行
    linux获知当前时间
  • 原文地址:https://www.cnblogs.com/gofighting/p/5036129.html
Copyright © 2011-2022 走看看