【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 ...
随机推荐
- linux常用方法
同步时间 ntpdate us.pool.ntp.org 查看http的并发请求数及其TCP连接状态 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in ...
- .NET常用方法收藏
1.过滤文本中的HTML标签 /// <summary> /// 清除文本中Html的标签 /// </summary> /// <param name="Co ...
- Env: Linux下Source Insight安装
1.Wine安装 sudo apt-get install wine 如果有错误,可以sudo apt-get update 2.下载source insight,注意要是安装版 http://www ...
- mysql 常用操作(整理)
原文地址:http://blog.csdn.net/lxh090821/article/details/9410943 1 登录数据库 格式: mysql -h主机地址 -u用户名 -p用 ...
- php全角字符转换为半角函数
<?php /** * 全角字符转换为半角 * * @param string $str * @return string public function Sbc2Dbc($str) { $ar ...
- 个人卡付款sql
select arap_djfb.billdate as 单据日期, bd_corp.unitname as 付款单位, arap_djfb.zy as 付款摘要, bd_cubasdoc.custc ...
- mysql给日期增减
有个需求就是判断过期的供求信息,如果用户刷新了则判断过期日期是否小于现在,如果是则自动推迟7天. IF(expire<NOW(),DATE_ADD(NOW(), INTERVAL 7 DAY), ...
- Android屏幕适配建议
一.关于布局适配 1.不要使用绝对布局 2.尽量使用match_parent 而不是fill_parent . 3.能够使用权重的地方尽量使用权重(android:layout_weight) 4.如 ...
- Think in UML笔记第1章--为什么要UML
1.1 面向过程还是面向对象 面向过程和面向对象都是一种软件技术.例如把面向过程归纳为结构化程序设计.DFD图.ER模型.UC矩阵等,而面向对象则被归纳为继承.封装.多态.复用等具体的技术.事实上,上 ...
- MySQL 绿色版安装方法图文教程
一.下载,这里使用绿色解压缩版 http://mirror.services.wisc.edu/mysql/Downloads/MySQL-5.1/mysql-noinstall-5.1.32-win ...