LeetCode: Roman to Integer 解题报告
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
SOLUTION 1:
思路:
从后往前遍历罗马数字,如果某个数比前一个数小,则把该数在结果中减掉;
反之,则在结果中加上当前这个数;
package Algorithms.string;
public class RomanToInt {
public int romanToInt(String s) {
if (s == null) {
return 0;
}
int len = s.length();
int sum = 0;
int pre = 0;
for (int i = len - 1; i >= 0; i--) {
int cur = romanTable(s.charAt(i));
if (i == len - 1) {
// 如果是在尾部,直接加上当前值
sum += cur;
} else {
// 判定当前值是不是比前一个值要小,如果小,则需要减去它
if (cur < pre) {
sum -= cur;
} else {
sum += cur;
}
}
pre = cur;
}
return sum;
}
public int romanTable(char c) {
int num = 0;
switch(c) {
case 'I':
num = 1;
break;
case 'V':
num = 5;
break;
case 'X':
num = 10;
break;
case 'L':
num = 50;
break;
case 'C':
num = 100;
break;
case 'D':
num = 500;
break;
case 'M':
num = 1000;
break;
default:
num = 0;
break;
}
return num;
}
}
SOLUTION 2:
除了用函数转换,也可以用map来转换。
public int romanToInt(String s) {
if (s == null) {
return 0;
}
// bug 1: forget new.
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int len = s.length();
int num = 0;
for (int i = len - 1; i >= 0; i--) {
int cur = map.get(s.charAt(i));
if (i < len - 1 && cur < map.get(s.charAt(i + 1))) {
num -= cur;
} else {
num += cur;
}
}
return num;
}
GITHUB 代码:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/RomanToInt.java
LeetCode: Roman to Integer 解题报告的更多相关文章
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- ITOO之底层关系
一.pom.xml文件关系图: 以基础系统中学生这条线为例: 图一:pom.xml文件关系图 图说明: 该项目是Maven项目,使用pom.xml文件对项目进行管理 管理类:(非阴影部分) Itoo- ...
- OpenWrt中对USB文件系统的操作, 以及读写性能测试
参考 http://h-wrt.com/en/doc/flash 1. 查看usb存储在启动日志中的信息 # dmesg [ 5.720000] usbcore: registered new int ...
- write()和prinln()的区别?
输出数字不同: write()输出数字转换为字符,println原样输出. 输出null不同: write()输出引用类型的时候调用的toString转换为String数据,因此如果对象为null那么 ...
- js 动态增加行删除行
<body> <table id="tableID" border="1" align="center" width=&q ...
- cxf之Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cxf.jaxrs.provider.ProviderFactory.<init>(Lorg/apache/cxf/Bus;)V
pom.xml中关于cxf的配置jar包冲突??? 1.http://blog.csdn.net/yzl_8877/article/details/53216923 2.https://www.cnb ...
- leetcode621 贪心:任务安排
题目链接 给定26种任务,每种任务的数量已知. 相同任务之间必须间隔n个时间段,为了不足n个时间段,可以让及其休息. 问:最少需要多长时间才能处理完这些任务? 这道题用贪心策略解决:每次安排任务时,优 ...
- 卸载linux订阅包
message日志信息: Oct :: oracledb1 rhsmd: In order for Subscription Manager to provide your system with u ...
- AME_AME审批中子元素的概念和用途(概念)
2014-05-30 Created By BaoXinjian AME: Oracle Approvals Management AME的6个元素的概念和主要作用: Attribue -> ...
- SVNserver搭建
SVN是Subversion的简称,是一个开放源码的版本号控制系统. 它由server和client组成,今天就带大家一起在server端搭建一个server. 前提:安装server端:Visual ...
- C++11新特性(1) 右值引用
在C++中,左值(lvalue)是能够获取其地址的一个量.因为常常出如今赋值语句的左边.因此称之为左值.比如一个有名称的变量. 比如: int a=10; //a就是一个左值. 传统的C++引用,都是 ...