题目:给定一个罗马数字串,转换为一个整数。

一开始没理解,以为是string to int。后来理解:罗马数字与阿拉伯数字的映射关系,见下图:

至此,题目的意思才掌握明白,用程序模拟这张表。

无可置否,需要将这张表的映射关系存进一个map中,对输入的string查找map中的映射关系。

先贴上代码:(注:substring(startIndex,endIndex) 截取的子字符串是从startIndex处到endIndex-1处为止的)

public int romanToInt(String s) {
Map<String , Integer> roman = new HashMap<String , Integer>();
roman.put("I" , 1);
roman.put("IV" , 4);
roman.put("V" , 5);
roman.put("IX" , 9);
roman.put("X" , 10);
roman.put("XL" , 40);
roman.put("L" , 50);
roman.put("XC" , 90);
roman.put("C" , 100);
roman.put("CD" , 400);
roman.put("D" , 500);
roman.put("CM" , 900);
roman.put("M" , 1000); int result = 0;
int len = s.length();
for(int i = 0 ; i < len ;){
if( len - i >= 2) {
String each = s.substring(i , i + 2);
if(roman.get(each) != null){
result += roman.get(each);
i = i + 2;
continue;
}
}
String each = s.substring(i , i + 1);
result += roman.get(each);
i = i + 1;
}
return result;
}

网络上的map很多只存了1,5,10,100,500,1000这几个值,但是需要一个辅助变量来管理,我觉得那样写绕来绕去的,很容易哪里就绕错了。

如果像我上面那样存1,4,5,9,10...进map。每次只需先判断两位,

while(遍历整个string){

  if(该两位string在map中存在)

    加上这个map值;

  else(如果该两位string在map中不存在)

    加上第一位string的map值;

}

清晰好理解。真的很好懂。

[leetcode]_Roman to Integer的更多相关文章

  1. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

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

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  3. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  4. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  5. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  6. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  7. 【一天一道LeetCode】#12 Integer to Roman

    一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...

  8. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  9. LeetCode题目_Reverse Integer

    最近在LeetCode上做题,写点东西记录一下,虽然自己做的都是些很水的题目,但是重在练手. 题号7:Reverse Integer,题目描述: Reverse digits of an intege ...

随机推荐

  1. [ActionScript] AS3代码实现渐变遮罩效果

    import flash.display.Shape; import flash.display.GradientType; import flash.geom.Matrix; import flas ...

  2. IE,Chrome滚动条样式CSS

    <style type="text/css"> *{ scrollbar-face-color:#F3F3F3; /*面子*/ scrollbar-arrow-colo ...

  3. MySQL 字符串 转 int/double CAST与CONVERT 函数的用法

    http://hongmin118.iteye.com/blog/2029728   转的 MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值.两者具体的语 ...

  4. C#委托多播、Lambda表达、多线程、任务

    class Program { static void Main(string[] args) { Action<double> ops = MathOperations.Mutiply; ...

  5. POJ 2524

    并查集思想,初始化每个元素的根节点为本身. 求解目标是求解存在几个集合.解决方案:查看有多少个根节点,表现在记忆数组上就是有多少个元素的根是它本身. #include<stdio.h> # ...

  6. [HDU 5074] Hatsune Miku (动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074 题目大意是给你m个note,n个数,得分是v[a[i]][a[i+1]]的总和,如果说a[i]是 ...

  7. C++ STL小知识

    五种迭代器: 在STL中,迭代器主要分为5类,分别是:输入迭代器.输出迭代器.前向迭代器.双向迭代器和随机访问迭代器. 输入迭代器 :只读,支持++.==.!=: 输出迭代器 :只写,支持++: 前向 ...

  8. Xcode自动注释插件

    开源xcode插件:规范注释生成器VVDocumenter 1.类似eclipse 和 vs studio 在前面输入/// 后触发,自动生成代码注释,如图 2.GitHub工程文件地址:https: ...

  9. Grunt 之 使用 JavaScript 语法检查工具 jshint

    前端开发环境准备好了,我们准备开始进行开发. 前端开发的主力语言是 JavaScript,这是一种脚本语言,没有编译器,也就没有了编译器带给我们的语法检查,怎样保证代码的质量呢?jshint 是一个强 ...

  10. Bootstrap 3 How-To #1 下载与配置

    Bootstrap 3 发布了,通过简单的几步,我们就可以使用 Bootstrap 的样式表,图标,以及 javascript 来配置一个简单的站点. 准备 Bootstrap 不仅仅是一个代码集,还 ...