Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

解法:

  只要考虑两种情况即可:

    第一,如果当前数字是最后一个数字,或者之后的数字比它小的话,则加上当前数字
    第二,其他情况(即为4或者9,这种情况下才可能出现后面的罗马数字比前面的大),则减去这个数字
public class Solution {
public int romanToInt(String s) {
if (s == null || s.length() == 0) return 0; int res = 0;
HashMap<Character, Integer> map = new HashMap<>();
map.put('M', 1000);
map.put('D', 500);
map.put('C', 100);
map.put('L', 50);
map.put('X', 10);
map.put('V', 5);
map.put('I', 1); for (int i = 0; i < s.length() - 1; i++) {
if (map.get(s.charAt(i)) >= map.get(s.charAt(i + 1))) {
res += map.get(s.charAt(i));
} else {
res -= map.get(s.charAt(i));
}
}
res += map.get(s.charAt(s.length() - 1)); return res;
}
}

[LeetCode] 13. Roman to Integer ☆☆的更多相关文章

  1. Leetcode#13. Roman to Integer(罗马数字转整数)

    题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  4. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

    1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  5. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  6. LeetCode 13. Roman to Integer(c语言版)

    题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...

  7. LeetCode 13 Roman to Integer 解题报告

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

  8. [leetcode]13. Roman to Integer罗马数字转整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  9. [leetcode] 13. Roman to Integer

    如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...

随机推荐

  1. 统计学习五:3.决策树的学习之CART算法

    全文引用自<统计学习方法>(李航) 分类与回归树(classification and regression tree, CART)模型是由Breiman等人于1984年提出的另一类决策树 ...

  2. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

    1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  3. 小程序的picker的range 是一个 Object Array (对象数组)

    小程序的picker的range 是一个 Object Array (对象数组) 数据: array: [{'id':1,'name':'Android'},{'id':2,'name':'IOS'} ...

  4. Python—元组(戴上了枷锁的列表)

    一.概念 元组属于不可变序列,元组是包含多个元素的类型,元素之间用逗号分割.例如:t1=123,456,“hello”元组可以是空的,t2=()元组包含一个元素时:t3=123,元组外侧可以使用括号, ...

  5. 给你的WP应用加上帮助文档

    背景 这算是Windows Phone编程回顾续篇, 接着给大家聊WP开发经验. 在开发了数个WP应用并发布后, 陆续收到很多反馈邮件, 其中接近一半的邮件是在问"某某功能有没有?" ...

  6. 【android】实现手指滑动来切换activity(转)

    http://code.eoe.cn/115 1.jpg外部引用 原始文档 MainActivity.java外部引用 原始文档 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  7. 对编码内容多次UrlDecode

    对编码内容多次UrlDecode,并不会影响最终结果. 尝试阅读了微软的源代码,不过不容易读懂. 网址:https://referencesource.microsoft.com/#System/ne ...

  8. LintCode-159.寻找旋转排序数组中的最小值

    寻找旋转排序数组中的最小值 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 你可以假设数组中不存在重复的 ...

  9. c++内存分类

    1. 代码段:放置代码 2. 静态数据段:放置全局变量和static的局部变量,字符串常量 3. 动态数据段:栈,放置局部作用域的变量,离开函数返回后就会被释放:堆,必须手动的分配和释放. 关于字符串 ...

  10. [剑指Offer] 47.求1+2+3+...+n

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). [思路]用&&的短路思想来求和 ...