【LeetCode】13. Roman to Integer 罗马数字转整数
题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路:与12题正好相反,罗马数字基本字符集:I V X L C D M (1, 5, 10, 50, 100, 500, 1000)。思路是从字符串最低位开始匹配,累加相应字符对应的阿拉伯数字,要注意的就是I,X,L(1,10,100)要判断是在左还是在右,在左就减在右就加。判断方法是看累加值是否分别大于5,50,500
public class Solution {
public int romanToInt(String s) {
char[] arr=s.toCharArray();
int val=0;
for(int i=arr.length-1;i>-1;i--){
switch(arr[i]){
case 'I':val+=val>=5?-1 : 1;
break;
case 'V':val+=5;
break;
case 'X':val+=10*(val>=50?-1:1);
break;
case 'L':val+=50;
break;
case 'C':val+=100*(val>=500?-1:1);
break;
case 'D':val+=500;
break;
case 'M':val+=1000;
break;
}
}
return val;
【LeetCode】13. Roman to Integer 罗马数字转整数的更多相关文章
- LeetCode 13 Roman to Integer(罗马数字转为整数)
题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description int toNumber(char ch) { switc ...
- [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] 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(罗马数字转整数)
这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...
- 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 ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- leetcode 13 Roman to Integer 罗马数组转整型
描述: 将一个字符串表示的罗马数字转为整数,范围0~3999 解决: 如果后一个比前一个大,则表示减,没什么技巧. map<}, {}, {}, {}, {}, {}, {}}; int rom ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
随机推荐
- sublime安装DocBlockr注释插件
点击sublime的菜单栏 view->show console :现在打开了控制台, 这个控制台有上下两栏, 上面一栏会实时显示sublime执行了什么插件,输出执行结果, 如果你安装的某个插 ...
- 使用水晶报表更新后出现“值不能为 null。 参数名: inputString”
简单记录一下: 如果更新完水晶报表相关页面可能在原来页面刷新会出现错误:"值不能为 null. 参数名: inputString",如图:
- 黄聪:Emeditor 编辑器常用的正则表达式
Emeditor 目前来说是我个人感觉非常不错的一款记事本软件, 其中查找替换功能由于支持正则表达式而显得非常强大. <tr[^>]*> 匹配:<tr xxxxxxxxxxxx ...
- 《黄聪:手机移动站SEO优化教程》3、如何禁止百度对PC网站进行自动转码
视频地址:http://v.youku.com/v_show/id_XNzE2OTM0NzU2.html
- Apache+php在windows下的安装和配置
下载和配置php 下载php:http://windows.php.net/download/ php-5.4.16-Win32-VC9-x86.zip 下载apache: http://ht ...
- 不用删除整个Maven本地库文件夹,Eclipse下直接更新Maven库
- ADF_Database Develop系列1_通过UML数据库开发之建Logical UML Class Model
2013-05-01 Created By BaoXinjian
- CE_现金预测详解(案例)
2014-07-14 Created By BaoXinjian
- NeHe OpenGL教程 第二十二课:凹凸映射
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Android屏幕适配建议
一.关于布局适配 1.不要使用绝对布局 2.尽量使用match_parent 而不是fill_parent . 3.能够使用权重的地方尽量使用权重(android:layout_weight) 4.如 ...