码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode Roman to integer

时间:2015-07-14 23:58:07      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:

题目:
Given a roman numeral, convert it to an integer.

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

要把罗马数字转换为整数, 罗马数字自行百度

code:

class Solution
{
  public:
    int romanToInt(string s)
    {
       map<char,int> Roman;
       Roman[‘I‘] = 1;
       Roman[‘V‘] = 5;
       Roman[‘X‘] =10;
       Roman[‘L‘] =50;
       Roman[‘C‘] =100;
       Roman[‘D‘] =500;
       Roman[‘M‘] =1000;
       int i=s.length()-1;
       int res;
       while(i>=0)
       {
          if(i==s.length()-1)
          {
            res=Roman[s[i]];
            i--;
          }

          if(Roman[s[i]] >= Roman[s[i+1]])
            res=res+Roman[s[i]];
          else
            res=res-Roman[s[i]];
          i--;
       }

       return res;
   }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode Roman to integer

标签:

原文地址:http://blog.csdn.net/nizhannizhan/article/details/46884897

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!