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 ...
随机推荐
- Java基础1-反射篇
1.页首请关注 思维导航大纲 2.大牛文章学习: 序号 博主 主要内容 1 sinat_38259539 总结的较全面的反射内容 2 3.自己的理解: 3.1.本文大纲 3.2.正文 1.获 ...
- C语言第一个例子hello world
1.用文本编辑器编辑代码如下,然后保存为hello.c文件 #include <stdio.h> int main(void){ printf("hello world" ...
- CitrixSmartAuditor安装报错解决方法
报错1:安装过程中报错 解决方法: SQLServer的配置: http://www.cnblogs.com/weizhengLoveMayDay/p/3267756.html 报错2:无法连接到Sm ...
- nnCron LITE
nnCron LITE is a small, but full-featured scheduler that can start applications and open documents a ...
- iOS开发之复制字符串到剪贴板
概述 一般有邀请复制链接需求功能,把字符串复制到系统剪贴板,供用户粘贴使用链接. 详细 代码下载:http://www.demodashi.com/demo/10714.html 一.主要思路 1.在 ...
- Android API之java.lang.String
boolean java.lang.String.contains(CharSequence cs) String字符串是否包含CharSequence(cs).
- MySQL5.7 二进制源码包安装
一般平时安装MySQL都是源码包安装的,但是由于它的编译需要很长的时间,所以建议安装二进制免编译包.可以到MySQL官方网站去下载,也可以到comsenz官方网站下载,还有各大镜像站下载. 下载安装包 ...
- 理解Lucene中的Analyzer
学习一个库,最好去官网.因为很多库API变动十分大,从博客上找的教程都过时了. Lucene原理就是简简单单的"索引",以空间换时间.但是Lucene将这件事做到了极致,后人再有想 ...
- Android开发简历书写的各个要点
对于我们这些自学成才的菜鸟来说,很多知识是欠缺的,比如如何写简历,今早上特意在网上学习了一下,写成学习笔记供大家参考. 篇幅,简历一般3页或者三页多一点是最好的,少了不好看,多了面试官不愿意看. 工作 ...
- Android中的Service使用
Service的生命周期 (适用于2.1及以上) 1. 被startService的无论是否有任何活动绑定到该Service,都在后台运行.onCreate(若需要) -> onStart(in ...