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 ...
随机推荐
- github下载源码的三种方式
从github上下载源码的三种方式 CreationTime--2018年6月7日15点21分 Author:Marydon 1.情景展示 2.实现方式 方式一:直接点击"Downloa ...
- 〖Linux〗使用gsoap搭建web server(C++)
1. gsoap的好处就不用说了:百度百科 2. gsoap的下载地址:项目地址,目前我使用的是2.8.15版本 3. 开发环境:Ubuntu13.10 4. 具体操作步骤(以简单相加为例): 1)编 ...
- 好用的eclipse properties插件
eclipse默认编辑器: 在有汉字的情况,特别是注释是汉字的情况,你会非常蛋疼的 JP的properties插件:http://propedit.sourceforge.jp/eclipse/upd ...
- Loadrunner脚本回放 场景运行过程中常见错误分析
问题一:Loadrunner超时错误问题描述 Loadrunner超时错误:在录制Web协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 问题现象Error -27728: ...
- Xiuno 开发手册正式发布。
下载地址:http://bbs.xiuno.com/down/xiuno.chm.tar.gz
- Linux命令-终止进程命令:pkill
killall是杀死所有进程,而pkill是按照进程名称杀死进程,可以达到杀死所有进程的目的,因为linux里面同名的进程是分主进程和子进程的. pkill - httpd 按名称强制杀死httpd进 ...
- OAF_OAF控件系列3 - Poplist的实现(案例)
2014-06-02 Created By BaoXinjian
- 在python3.x下使用如下代码: import cPickle as pk 报错
在python3.x下使用如下代码: import cPickle as pk会报如下错误:ImportError: No module named 'cPickle' 原因:python2有cPic ...
- django web 笔记
安装 django pip install django 创建虚拟环境 python -m venv testenvironment 进入虚拟环境: testenvironment\Script ...
- MySQL "replace into" 的坑以及insert相关操作
下面我们主要说一下在插入时候的几种情况: 1:insert ignore 2:replace into 3:ON DUPLICATE KEY UPDATE 关于insert ignore: 关于rep ...