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

Roman to Integer

时间:2015-11-12 11:41:31      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

题目:

Given a roman numeral, convert it to an integer.

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

解析:

这题没兴趣做,抄答案

http://blog.csdn.net/jellyyin/article/details/13165731

 1 class Solution {
 2 public:
 3     int romanToInt(string s) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         int result=0;
 6         
 7         map<char,int> roman;
 8         roman[I]=1;
 9         roman[V]=5;
10         roman[X]=10;
11         roman[L]=50;
12         roman[C]=100;
13         roman[D]=500;
14         roman[M]=1000;
15         
16         for(int i=s.length()-1;i>=0;i--)
17         {
18             if(i==s.length()-1)
19             {
20                 result=roman[s[i]];
21                 continue;
22             }
23             if(roman[s[i]] >= roman[s[i+1]])
24                 result+=roman[s[i]];
25             else
26                 result-=roman[s[i]];
27         }
28         return result;
29     }
30 };

 

Roman to Integer

标签:

原文地址:http://www.cnblogs.com/raichen/p/4958169.html

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