【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 ...
随机推荐
- POM (Project Object Model)简介
1 概念介绍 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它使用 java1.5 来编译. < ...
- Windows2012 cannot access netapp CIFS share
NAS1> options cifs.smb2.signing.requiredcifs.smb2.signing.required off NAS1> options cifs.smb2 ...
- 【log】logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 控制台 - ...
- Linux常用命令记录
产用Linux命令 cat /proc/partitions //查看系统分区情况 fdisk -l /dev/sdb //查看磁盘物理存储 mount /dev/目录 /mnt/目录 //挂载文件 ...
- 网页爬虫--scrapy入门
本篇从实际出发,展示如何用网页爬虫.并介绍一个流行的爬虫框架~ 1. 网页爬虫的过程 所谓网页爬虫,就是模拟浏览器的行为访问网站,从而获得网页信息的程序.正因为是程序,所以获得网页的速度可以轻易超过单 ...
- BestCoder Round #87 LCIS(dp)
LCIS 要用dp的思路想这题 [题目链接]LCIS [题目类型]dp &题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的,比如(x,x+1,...,y−1 ...
- NeHe OpenGL教程 第十四课:图形字体
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第七课:光照和键盘
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 别去研究C++
转载 YH,今天早晨起来.回想昨天,虽然吐槽了 C++ 的各种问题,但给别人打工,还是要靠 C++ 干活吃饭.我对待 C++ 的态度和云风不同,虽然他所说的 C++ 技术的事情我都懂都理解,而我感受到 ...
- React Native 开发。
1.react-native run-android 安装 2.react-native start 开启调试端口