LeetCodeOJ刷题之13【Roman to Integer】
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
意思就是:
给出一个罗马数字,返回其对应的整数 num( 0<=num<=3999)表示;
罗马数字范围从 0-->3999
罗马数字有:
I | V | X | L | C | D | M |
---|---|---|---|---|---|---|
1 | 5 | 10 | 50 | 100 | 500 | 1000 |
Solutions
-
- 这种方法是对罗马数字从头到尾的扫描一遍。分析一下可以知道,只有 4 和 9 才会出现小一点的罗马数字出现在大的罗马数字的前面!比如:
CD=400
其中C=100 , D=500
,此时需要的后一个罗马数字减去前面一个罗马数字,其他情况都是一直加的。代码如下:
class Solution {
public:
int romanToInt(string s) {
int idx=0,n=s.length();
if(s.length()<=0)return 0; string roman="IVXLCDM";
int nums[] ={1,5,10,50,100,500,1000};
int res=0;
//idx=roman.find(s.at(0));
//if(idx<0||idx>6)return 0;
//if(n==1){
//return nums[idx];
//}
int num1,num2;
idx=0;
while(idx<n-1){ // 需要考虑 idx+1 的越界情况
num1=roman.find(s[idx]);
num2=roman.find(s[idx+1]);
if(num2>num1){
res=res+(nums[num2]-nums[num1]);
idx+=2;
}else{
res=res+nums[num1];
++idx;
}
}
if(n-1==idx){ //需要考虑可能剩下的最后一个罗马数字
num1=roman.find(s[n-1]);
res+=nums[num1];
}
return res;
}
}; - 这种方法是对罗马数字从头到尾的扫描一遍。分析一下可以知道,只有 4 和 9 才会出现小一点的罗马数字出现在大的罗马数字的前面!比如:
-
- 方案1中需要考虑 idx+1 的越界情况,所以还要在最后再增加一个判断,且使用的是从前向后扫描罗马数字字串;那如果是从后向前扫描罗马数字字串呢?思想会不会简单一点呢?分析一下可以看出,从后向前扫描,可以一直加下去,除了一种情况:当前扫描的数小于上一个扫描的数!也就是说只有这一个条件了,不需要再判断什么越不越界的问题了。如:
MCMVI=1906
, 扫描V
时V>I
(在罗马数字中,以下同),所以直接加,结果为:6,扫描M
也一样,结果为1006,接下来扫描到C
,此时C<M
,所以应是减去C
,结果是906,最后再加M
,得到结果:1906。要做的,只是记录下上一次的罗马数字。代码如下:
class Solution {
public:
int romanToInt(string s) {
if(s.length()<=0)return 0;
string roman="IVXLCDM";
int nums[] ={1,5,10,50,100,500,1000};
int n=s.length();
int res=0, idx=n-1, pre=0, ndx;
while(idx>=0){
ndx=roman.find(s[idx]);
if(pre>ndx){
res-=nums[ndx];
}else{
res+=nums[ndx];
}
pre=ndx;
--idx;
}
return res;
}
};
- 方案1中需要考虑 idx+1 的越界情况,所以还要在最后再增加一个判断,且使用的是从前向后扫描罗马数字字串;那如果是从后向前扫描罗马数字字串呢?思想会不会简单一点呢?分析一下可以看出,从后向前扫描,可以一直加下去,除了一种情况:当前扫描的数小于上一个扫描的数!也就是说只有这一个条件了,不需要再判断什么越不越界的问题了。如:
-
- 查阅了网友的解答,发现这一个方式挺好的,思路也很简单:
- 逐个遍历罗马数字串,根据当前遍历到的字符,进行对应的累加操作。
- 罗马数字共 7 个符号,
IVXLCDM
,如果遍历到罗马数字 w ,则判断之前已经求出的数字 res 是否小于 w ,如果小于,则 加上 w :res=res+w
,否则减去 w 。原因是:因为是从后向前遍历,所以我们已经遍历出来的数字肯定是会小于下一进位的数字的,比如 189 ,89再大,也没有100大!所以此算法使用的就是这种思想,一旦出现违法了这种情况,那就是出现了 4 或者 9 的情况。说明我们加多了,那就要减去。说的可能比较不好完全理解,看代码:
class Solution {
public:
int romanToInt(string s) {
int res = 0 ,n=s.length();
for (int i = n - 1; i >= 0; --i) {
char c = s.at(i);
switch (c) {
case 'I':
res += (res >= 5 ? -1 : 1);
break;
case 'V':
res += 5;
break;
case 'X':
res += 10 * (res >= 50 ? -1 : 1);
break;
case 'L':
res += 50;
break;
case 'C':
res += 100 * (res >= 500 ? -1 : 1);
break;
case 'D':
res += 500;
break;
case 'M':
res += 1000;
break;
}
}
return res;
}
};
- 查阅了网友的解答,发现这一个方式挺好的,思路也很简单:
看了一下其他的代码,大致的思想都是上面的方法,可能还有我没发现的。只是代码编写的形式不同而已。
附录
同系列:LeetCodesOJhttp://www.cnblogs.com/lomper/tag/LeetCodesOJ/
LeetCodeOJ刷题之13【Roman to Integer】的更多相关文章
- 【leetcode刷题笔记】Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- leecode刷题(13) -- 字符串中的第一个唯一字符
leecode刷题(13) -- 字符串中的第一个唯一字符 字符串中的第一个唯一字符 描述: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = & ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode easy #13 Roman to Integer
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【LeetCode】13. Roman to Integer (2 solutions)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- 小a与“204”------数列、排序
链接:https://ac.nowcoder.com/acm/contest/317/B来源:牛客网 小a非常喜欢204204这个数字,因为′a′+′k′=204′a′+′k′=204. 现在他有一个 ...
- centos 7 查看系统版本信息
2018-11-06 1. 查看版本号 CentOS的版本号信息一般存放在配置文件当中,在CentOS中,与其版本相关的配置文件中都有centos关键字,该文件一般存放在/etc/目录下,所以说我们 ...
- java c c++大学补遗
第一次面试时的问题是一个看起来50多数的老工程师问的, 仍然记忆犹新 java(面向对象)的基本特性? 封装 继承 多态 工作几年后,各种框架用来用去, 回想起这个问题,java也就剩下这几个特性了
- 网络编程api bind函数细节 select 细节
struct sockaddr_in bindaddr; bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ ...
- Python Fileinput 模块
作者博文地址:http://www.cnblogs.com/liu-shuai/ fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行. [默 ...
- 求入栈顺序为1234……N的序列的所有可能的出栈序列
class Program { private static void Fun(int x, int n, Stack<int> stack, List<int> outLis ...
- python 模板注入
今天学习了python的模板注入,这里自己搭建环境测试以下,参考文章:http://www.freebuf.com/articles/web/136118.html web 程序包括两个文件: fla ...
- C++程序设计基础(6)内存分配
1.知识点 三步走:申请,释放,指针置空. 1.1malloc.free函数 在C语言中内存malloc函数申请动态空间,以下展示其基本用法: int *p = NULL; p = ();//申请 f ...
- VS 连接数据库报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
VS报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...
- tomcat和应用集成
将tomcat作为应用的一部分集成到应用中,使得应用可以直接开启http服务,对外提供接口.此时应用程序不必再遵守j2ee中的文件目录格式要求. 此种方式改变了以往先部署tomcat容器,再按照j2e ...