LeetCode:12. Roman to Integer (Easy)
1. 原题链接
https://leetcode.com/problems/roman-to-integer/description/
2. 题目要求
(1)将罗马数字转换成整数;(2)范围1-3999;
3. 关于罗马数字
罗马数字相关规则已经在之前一篇博客里写过,这里不再赘述(之前博客的传送门)
4. 解题思路
(1)这与之前第十二题Integer转换Roman虽然很相似,但处理方法并不相同。罗马数字更像是一个字符串,因此将其转换成字符数组进行处理。
(2)从后向前遍历一次,结合罗马数字的组合规则,根据该位置罗马数字与之前位置累加起来的和进行大小比较,判断该位置是加还是减。
5. 代码实现
public class RomantoInteger13 {
public static void main(String[] args) {
System.out.println(RomantoInteger13.romanToInt("MDCCCLXXXIV"));
}
public static int romanToInt(String s) {
int count = 0;
for(int i = s.length()-1;i>=0;i--){
char c = s.charAt(i);
switch (c){
case 'I':
count += (count>=5?-1:1);
break;
case 'V':
count +=5;
break;
case 'X':
count+=(count>=50?-10:10);
break;
case 'L':
count+=50;
break;
case 'C':
count+=(count>=500?-100:100);
break;
case 'D':
count+=500;
break;
case 'M':
count+=1000;
break;
} } return count; }
}
LeetCode:12. Roman to Integer (Easy)的更多相关文章
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
- 「Leetcode」13. Roman to Integer(Java)
分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- Java-Runoob-高级教程-实例-方法:12. Java 实例 – Enum(枚举)构造函数及方法的使用-um
ylbtech-Java-Runoob-高级教程-实例-方法:12. Java 实例 – Enum(枚举)构造函数及方法的使用 1.返回顶部 1. Java 实例 - Enum(枚举)构造函数及方法的 ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- Windows Phone本地数据库(SQLCE):12、插入数据(翻译)
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十二篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...
- LeetCode:7. Reverse Integer(Easy)
题目要求:将给出的整数进行逆序输出 注意:整数的最大范围-2147483648-2147483647,当翻转后的数超出范围后返回0 思路:对给出的整数除以10,取余和取整:然后对取整部分继续取余和取整 ...
- LeetCode:5. Longest Palindromic Substring(Medium)
原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 ...
随机推荐
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(三) 激动人心的时刻到啦,实现1v1聊天
看起来挺简单,细节还是很多的,好,接上一篇,我们已经成功连接singalR服务器了,那么剩下的内容呢,就是一步一步实现聊天功能. 我们先看看缺什么东西 点击好友弹框之后,要给服务器发消息,进入组Gro ...
- 原生ajax、XMLHttpRequest和FetchAPI简单描述
什么是ajax ajax的出现,刚好解决了传统方法的缺陷.AJAX 是一种用于创建快速动态网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个 ...
- 【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解
题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...
- PAT1064. Complete Binary Search Tree
1064. Complete Binary Search Tree 题目大意 给定一个序列, 求其 生成Complete BST 的层序遍历. 思路 最开始把这个题想复杂了, 还想着建立结构体, 其实 ...
- JavaScript:直接写入 HTML 输出流
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- android SQLITE的基本使用总结(八)
sharedPreferences只适合存储比较简单的数据和键值对,支持不同的数据类型 文件存储连键值对都没有,不会进行任何格式化处理,存储简单的二进制或者文本数据 sqlite则能处理一些数据量大, ...
- 2017-09-26 发布 SpringBoot多模块项目实践(Multi-Module)
https://segmentfault.com/a/1190000011367492?utm_source=tag-newest 2017-09-26 发布 SpringBoot多模块项目实践(Mu ...
- linux 怎么查看系统的环境变量 与设置jdk 系统环境变量
1.win 7 ,win10 怎么查看,添加系统环境的变量,大家都非常清楚的.但是linux 的 却不一定哦. 打开终端输入 : “echo $PATH “ or “export ” 如 ...
- Showing All Messages : error: open /Users/apple/Library/Developer/Xcode/DerivedData/xxx-dkhmpttmnuppvbcxijlcxacfpzcl/Build/Products/Debug-iphoneos/xxx.app/EaseUIResource.bundle/arrow@2x.png: N
2报错 Showing All Messages : error: open /Users/apple/Library/Developer/Xcode/DerivedData/xxx-dkhmpttm ...
- 理解 ES6 Generator-next()方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...