`
frank-liu
  • 浏览: 1666049 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: Roman to Integer

 
阅读更多

问题描述:

Given a roman numeral, convert it to an integer.

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

原问题链接:https://leetcode.com/problems/roman-to-integer/

 

问题分析

    在前面的文章中已经提到过罗马数字的组成规则。它本质上是由有限的单个字符组成。针对不同的位以及重复次数限制来组成别的数字。它们基本的字符无非就是如下几个:

I: 1    V: 5    X: 10    L: 50    C: 100    D: 500    M: 1000

    所以给定一个罗马数字,无非就是要将一串罗马数字给解析成对应的阿拉伯数字并将结果加起来。这里该如何对给定的罗马数字串解析就是问题的核心。在罗马数字的表示方法里,对于像4, 9之类的数字不管在个位十位百位等,它们都是一个小的数字放在一个大的数字前面。而对于其他的数字情况,无非就是几个重复的数字。所以,当给定一个当前的罗马数字,如果它比它后面的数字小,则应该将它们放在一起解析。否则就单独解析,然后加起来就可以了。因为我们是一个字符一个字符的解析,对于出现前面一个数字不小于后面的就直接相加,而否则的情况则要减一个对应的值。而为了描述这个计算,我们可以将单个的罗马数字符和对应的阿拉伯数字映射起来。在具体的实现里可以用一个map来保存。

    这样我们就得到一个如下的实现:

 

public class Solution {
    public int romanToInt(String s) {
        if (s == null || s.length() == 0) return -1;
        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);
        if(s.length() == 1) return map.get(s.charAt(0));
        int result = 0;
        for (int i = 1; i < s.length(); i++) {
            if (map.get(s.charAt(i - 1)) >= map.get(s.charAt(i)))
                result += map.get(s.charAt(i - 1));
            else
                result -= map.get(s.charAt(i - 1));
        }
        result += map.get(s.charAt(s.length() - 1));
        return result;
    }
}

     在这个实现里要考虑到串长度为1的情况。另外,针对和前一个字符的比较,我们需要把最后的那个字符给加进来以免遗漏。这个方法的实现看起来很简单,但是实际执行效率并不是很高。为什么会这样呢?因为我们这里是建立的字符到Integer整数的映射。但是每次访问解析的时候要将值做一个boxing, unboxing的操作。这样将极大的降低执行的效率。这其实也是java语言本身的一些限制导致的。

    没办法,如果要想让上述的代码再快一点,可以将字符串的映射转换成一个函数,然后用一大堆的判断语句来返回结果。这样会快不少。因为比较简单,这种实现就不赘述了。

分享到:
评论

相关推荐

    LeetCode Roman to Integer解决方案

    LeetCode Roman to Integer解决方案

    Roman to Integer完整代码

    leetcode上Roman to Integer的完整C++代码,已被accepted

    lrucacheleetcode-LeetCode:本库有各种在线编码平台几个问题的解决方案

    lru缓存leetcode 力码 涵盖了 Geeks for Geeks 和 Leet Code 的各种问题。...Integer (249_Easy) LeetCode 14 : 最长公共前缀 (250_Easy) LeetCode 15 : 3Sum (271_Medium) LeetCode 17 : 电话号码的

    javalruleetcode-LeetCode:LeetCode算法问题

    java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...Integer LeetCode 6 Zi

    leetcode卡-LeetCode:LeetCode题解

    leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- ...

    leetcode中国-leetcode:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

    qycl50224#leetcode#13. 罗马数字转整数 Roman to Integer1

    13. 罗马数字转整数 Roman to Integer用哈希存储映射字符---&gt;对映的值对字符串的字符挨个判断,考虑下一个字符如果下一个字符大于当前字符,su

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...

    leetcode题库-LeetCode:力码

    leetcode题库 LeetCode 题解合集 本仓库展示了LeetCode题库中部分题目的解法(持续更新),所有代码均采用C++编写并配有输入输出样例 代码清单如下: 序号 题目 程序源码 1 两数之和 Two Sum.cpp 2 两数相加 Add Two...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode答案-LeetCode:Swift中的LeetCode

    Roman to Integer Easy #21 Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray ...

    leetcode2sumc-leetcode:JavaScript版本leetcode中文版代码

    leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript 难度 1 Two Sum 简单 2 Add Two Numbers 中等 3 Longest Substring Without Repeating... 中等 5...

    leetcode叫数-leetcode:leetcode

    Roman to Integer 查表,通过从前往前筛选字符串,把代表的值一个个加起来 26. Remove Duplicates from Sorted Array 难度easy的题目。根据题目要求,是不能新建数组。只能在原来的基础上做修改。基本上这个算法类似...

    leetcode中文版-LeetCode:LeetcodeC++/Java

    Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个有序链表 lin

    lrucacheleetcode-LeetCode:我从LeetCode提交的一些任务

    (/problems/integer-to-english-words/) 22.1% 难278 25.4% 容易17 34.7% 中91 19.7% 中10 正则表达式匹配 (/problems/regular-expression-matching/) 24.1% 难253  38.9% 中15 21.6% 中277 寻找名人 (/problems/...

    leetcode答案-LeetCode:重做一遍LeetCode

    leetcode 答案力码 做LeetCode 1.【二和】(Array & Hash Table) (Easy) 20190911 完成 2.【加两个数】(链表&数学)(中) ...3.【无重复字符的最长子串...13.【Roman To Integer】(中) 20181106完成的IIV是非法的? 14.

    javalruleetcode-leetcode:leetcode问题的解决方案

    java lru leetcode Leetcode 问题的解决方案 问题 ...0012_Integer_to_Roman 0013_Roman_to_Integer 0014_Longest_Common_Prefix 0015_3总和 0016_3Sum_Closest 0017_Letter_Combinations_of_a_Phone_N

    leetcodepython001-LeetCode:力码

    013_Roman_to_Integer 2021 年 1 月 9 日 Python 005 014_Longest_Common_Prefix 5 月 12 日。 2021年 Python 006 020_Valid_括号 5 月 12 日。 2021年 Python 007 021_Merge_Two_Sorted_Lists 2021 年 6 月 10 日 ...

    LeetCode判断字符串是否循环-LeetCode:LeetCode问题的Javascript解决方案

    LeetCode判断字符串是否循环 LeetCode Javascript Solutions to problems ...Roman to Integer Easy 15 Medium 21 Easy 26 Remove Duplicates from Sorted Array Easy 142 Medium 191 Number of 1 Bit

Global site tag (gtag.js) - Google Analytics