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 ...
随机推荐
- numpy教程:快速傅里叶变换模块numpy.fft
http://blog.csdn.net/pipisorry/article/details/51050297 快速傅里叶变换 NumPy中,fft模块提供了快速傅里叶变换的功能.在这个模块中,许多函 ...
- Android进阶(四)一个APP引发的思索之ArrayList的add总是添加相同的值
解决"ArrayList的add总是添加相同的值"问题 前言 最近在写一个小的Android APP,在用ArrayList的add时,总是出现添加相同值的现象.如下图所示: 错误 ...
- jQuery 异步上传插件 Uploadify302 使用 (JavaEE Spring MVC)
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.而且是Ajax的,省去了自己写Ajax上传功能的麻烦.不过官方提供的实例时php版本的,本文将详细介绍Uploadify ...
- pig代码格式上小注意
1,%default file test.txt 中不要用引号,'' 和""都不行.'file'不会被识别 2,pig判断相等,用==,不是一个=.. 3,pig中只用单引号,不用 ...
- XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容
一.新建Demo2类: import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPar ...
- HTML5 预加载
原文地址: HTML5 Link Prefetching 原文日期: 2010年07月07日 翻译日期: 2013年08月13日 浏览器厂商和开发者之间共同努力的一个方向就是让网站更快.现在已有很多广 ...
- SQL备份所有数据库脚本
技巧要点:使用游标循环读取所有数据库名,然后定义存放路径,最后备份所有数据库到指定存在的本地文件夹中 脚本如下: declare @fileName varchar(255) --定义备份文件名变量d ...
- Leetcode_28_Implement strStr
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41452047 Implement strStr(). Re ...
- React Native之AppRegistry模块
我们在写react native的js的时候,在最后总会加上一段代码: AppRegistry.registerComponent('ReactDemo', () => ReactDemo); ...
- HEVC(H.265)标准的编码器(x265,DivX265)试用
基于HEVC(H.265)的的应用级别的编码器发展的速度很快.所说的应用级别,就是指速度比较快的,有实际应用价值的编码器.目前可以直接使用的有两个:x265,DivX265. DivX265 DivX ...