Roman to Integer(将罗马数字转成整数)
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
将罗马数字转成阿拉伯数字。需要了解两者对应关系。
罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。
1、重复数次:一个罗马数字重复几次,就表示这个数的几倍。
2、
2.1 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
2.2 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。
利用规则,可以将字符串对应字符转成对应的数字,存放在数组中,然后将数组中数字相加,相加的时候要满足2.2,即当后一个元素大于前一个元素,表示后一个元素减去前一个元素,也就是和减去前一个元素,加上后一个元素。见代码
tag:Math,String
class Solution {
public int romanToInt(String s) {
int[] a=new int[s.length()];
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
if(ch=='I') {a[i]=1;continue;}
if(ch=='V') {a[i]=5;continue;}
if(ch=='X') {a[i]=10;continue;}
if(ch=='L') {a[i]=50;continue;}
if(ch=='C') {a[i]=100;continue;}
if(ch=='D') {a[i]=500;continue;}
if(ch=='M') {a[i]=1000;continue;}
}
int sum=0;
for(int i=0;i<a.length-1;i++){
if(a[i]<a[i+1])
sum-=a[i];
else
sum+=a[i];
}
return sum+a[a.length-1]; //这里因为上面的循环没有操作最后一个数,所以加上最后一个数
}
}
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(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- 13. Roman to Integer[E]罗马数字转整数
题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- [Leetcode] String to integer atoi 字符串转换成整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [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 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- 8. String to Integer (atoi) 字符串转成整数
[抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...
随机推荐
- 最简单的基于DirectShow的示例:视频播放器自定义版
===================================================== 最简单的基于DirectShow的示例文章列表: 最简单的基于DirectShow的示例:视 ...
- oralce 查看是否启动 登陆 创建用户 常用命令小记
最简单看进程有没有: ps -ef | grep ora 其次用oracle的的命令查看,比如: su - oracle sqlplus / as sysdba 看能连进数据库不. 创建用户和表空间: ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(六):放置地鼠
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- Java数据类型及类型转换
http://blog.csdn.net/pipisorry/article/details/51290064 java浮点数保留n位小数 import java.text.DecimalFormat ...
- BFS与DFS总结
最近一直在看DFS和BFS,感觉要晕的GJ. DFS思想: 一直往深处走,直到找到解或者走不下去为止 DFS框架: DFS(dep,-) //dep代表目前DFS的深度 { if (找到 ...
- Aandroid TV 基于Leanback支持最新MD设计的TV开发框架
原文地址:http://blog.csdn.net/sk719887916 作者:skay 基于6.0最新的API 支持TV的框架 Android 6.0已完美支持TV开发,之前的5.0后Recycl ...
- iOS开发xcode报错:"xxxxxx"has been modified since the precompiled header was built
最近做iOS开发,老是遇到这种问题,正好我有两个版本的Xcode,换了另一个,就正常了,所以也一直没解决. 今天又遇到这个问题,就查了一下资料,找到了解决办法,在次记录一下方便查找,就是在Xcode中 ...
- c# 单元测试工程如何取得当前项目路径
前言: C#工程项目中有些配置文件,数据文件等不是C#工程文件,但是程序中需要访问,如果写成绝对路径不利于程序的迁移,所以必须写成相对路径.取得相对路径的方法很多,网上的例子也很多,基本上是七种吧,这 ...
- Android读取/dev/graphics/fb0 屏幕截图
Android屏幕截图有很多方式这里只使用其中一种截图 主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换. 可以参考一下这篇文章:http://blog.ch ...
- Android开发技巧——TextView加载HTML的图片及代码显示问题
前几天在做一个Gradle用户指南的应用程序,使用的是TextView来加载HTML内容(至于为什么不用WebView,我也没有认真使用并比较过,也许以后会换吧),其中遇见了一些纠结的问题,所幸主要的 ...