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. Oracle 跨库查询表数据(不同的数据库间建立连接)

      1.情景展示 当需要从A库去访问B库中的数据时,就需要将这两个库连接起来: 两个数据库如何实现互联互通,在oracle中,可以通过建立DBLINK实现. 2.解决方案 2018/12/05 第一步 ...

  2. SDUT 1157-小鼠迷宫问题(BFS&amp;DFS)

    小鼠迷宫问题 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1500ms Memory Limit 65536 ...

  3. spring注解配置quartz

    常规配置quartz可以参考我的另外一篇博文:http://www.cnblogs.com/yangzhilong/p/3349116.html spring配置文件里增加: 命令空间: http:/ ...

  4. spring mvc自定义数据转换

    @InitBinder   在controller中注册一个customer protperty editor以解析request中的参数并通过date bind机制与handler method中的 ...

  5. smack 4.1.2+openfire 3.10.2i

    openfire 和以往版本号配置没有多大差别就不具体介绍了,网上搜会有一大堆的图解 以下主要说一下smack 4.1.2 的开发使用,在网上看了好多文章包含stackoverflow的都没有4.1以 ...

  6. 一些很经典的JavaScript的问题

    1.作用域 (function() { var a = b = 5; })(); console.log(b); 输出:5 陷阱是,在函数表达式中有两个赋值,但a是用关键字var 来声明的,这意味着a ...

  7. iOS获取手机型号,Swift获取手机型号(类似iphone 7这种,检测机型具体型号)

    获取手机设备信息,如name.model.version等, 但如果想获取具体的手机型号,如iphone5.5s这种,就需要如下这种(含Swift和OC两种写法) Swift建议添加到extensio ...

  8. CentOS6.6 32位 Minimal版本纯编译安装Nginx Mysql PHP Memcached

    声明:部分编译指令在博客编辑器里好像被处理了,如双横线变成单横线了等等,于是在本地生成了一个pdf版本,在下面地址可以下载. LNMP+Memcached CentOS是红帽发行的免费的稳定Linux ...

  9. Linux调度器 - 进程优先级

    一.前言 本文主要描述的是进程优先级这个概念.从用户空间来看,进程优先级就是nice value和scheduling priority,对应到内核,有静态优先级.realtime优先级.归一化优先级 ...

  10. 深度解析(一六)Floyd算法

    Floyd算法(一)之 C语言详解 本章介绍弗洛伊德算法.和以往一样,本文会先对弗洛伊德算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 弗洛伊德 ...