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 ...
随机推荐
- iOS下WebRTC音视频通话(三)-音视频通话
前两篇文章记录了音视频通话的一些概念和一些流程,以及一个局域网内音视频通话的示例. 今天以一个伪真实网络间的音视频通话示例,来分析WebRTC音视频通话的过程. 上一篇因为是在相同路由内,所以不需要穿 ...
- hashmap 循环取出所有值 取出特定的值 两种方法
//第一种 Iterator menus = menu.iterator(); while(menus.hasNext()) { Map userMap = (Map) menus.next(); S ...
- C语言--static修饰变量
Static在C语言里面有两个作用,第一个是修饰变量,第二个是修饰函数. 1.Static修饰变量 按照作用范围的不同,变量分为局部变量和全局变量.如果用static修饰变量,不论这个变量是全局的还是 ...
- UE4利用Save Game创建全局变量
因为盲目的做了一个UE4的项目,没有用到UE4的无缝加载,我只能在一个个关卡中手动切换,然后每次的数据都会重置,这对于项目来说,造成了体验感的极度下降. 然而我查了一下怎样在UE4中创建全局变量,找到 ...
- UNIX网络编程——socket概述和字节序、地址转换函数
一.什么是socket socket可以看成是用户进程与内核网络协议栈的编程接口.socket不仅可以用于本机的进程间通信,还可以用于网络上不同主机的进程间通信. socket API是一层抽象的网络 ...
- MySQL数据库安装与配置详解(图文)
接下来看一下如何安装mysql数据库. 由于有更详细的教程资源,因此参考别人的文章以整理.安装教程参考自博客园文章http://www.cnblogs.com/sshoub/p/4321640.htm ...
- [sersync+rsync] centos6.5 远程文件同步部署记录
针对本地文件的修改,自动同步到远程文件夹,远程备份很方面.研究了下大家的主流同步方案一般是 rsync+inotify和rsync+sersync, 我这里使用sersync的方案,当然大部分都是参照 ...
- GROUP BY 的实现与优化
由于GROUP BY实际上也同样需要进行排序操作,而且与ORDER BY相比,GROUP BY主要只是多了排序之后的分组操作.当然,如果在分组的时候还使用了其他的一些聚合函数,那么还需要一些聚合函数的 ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十六)
回到MainScene.m中添加selectRobot方法: -(void)selectRobot:(Robot *)robot{ LevelRestrict *lr = [LevelRestrict ...
- 基于HTTP头部的注入
基于HTTP头部的注入 常见的sql注入一般都是通过表单或请求参数进行注入,但这里给出的例子是通过HTTP协议头部进行注入. 例如一个的请求如下: GET / HTTP/1.1 Host: www.e ...