首页 > 其他 > 详细

Roman to Integer

时间:2015-11-12 11:40:28      阅读:255      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!