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 解题报告的更多相关文章

  1. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. Java学习笔记五(多线程)

    1.介绍 线程可以使程序具有两条和两条以上的可运行的路径.尤其对多核CPU特别的重要. 2.创建线程 1.继承Thread类 一个类直接的继承Thread类的话,此类就具有了线程的能力,接下来仅仅须要 ...

  2. 〖Linux〗Ubuntu13.10,声音图标调节音量失效的解决办法

    升级Ubuntu13.10,发现声音图标不能调节音量[XUbuntu13.10发行日志]: 临时解决办法: gvim /usr/share/dbus-1/services/indicator-soun ...

  3. MySQL存储过程中的3种循环【转载】

    在MySQL存储过程的语句中有三个标准的循环方式:WHILE循环,LOOP循环以及REPEAT循环.还有一种非标准的循环方式:GOTO,不过这种循环方式最好别用,很容易引起程序的混乱,在这里就不错具体 ...

  4. 使用Nginx限制同一IP的访问频率

    http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html http://nginx.org/en/docs/http/ngx_http ...

  5. IBM InfoSphere DataStage 8.1 DataStage Job 开发具体解释

    简单介绍 DataStage 使用了 Client-Server 架构,server端存储全部的项目和元数据,client DataStage Designer 为整个 ETL 过程提供了一个图形化的 ...

  6. (原)Ubuntu16中安装nvidia的显卡驱动

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638185.html part1 直接在“软件和更新-附加驱动”里面设置 安装完ubuntu16后,显 ...

  7. linux显示桌面快捷键设置

    2013-01-06 10:31:52    Ubuntu显示桌面Indicator  IN: LINUX :-) HOT: 1,246 ℃  18十2011   www.2cto.com   大家一 ...

  8. HDUOJ---1233还是畅通工程

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. Foundations of Machine Learning: Rademacher complexity and VC-Dimension(2)

    Foundations of Machine Learning: Rademacher complexity and VC-Dimension(2) (一) 增长函数(Growth function) ...

  10. 用Python实现BP神经网络(附代码)

    用Python实现出来的机器学习算法都是什么样子呢? 前两期线性回归及逻辑回归项目已发布(见文末链接),今天来讲讲BP神经网络. BP神经网络 全部代码 https://github.com/lawl ...