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

一开始没理解,以为是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. C++学习21 基类和派生类的赋值

    在C/C++中,经常会发生数据类型转换,例如整型数据可以赋值给浮点型变量,在赋值之前,先把整型数据转换为浮点型:反过来,浮点型数据也可以赋值给整型变量. 数据类型转换的前提是,编译器知道如何对数据进行 ...

  2. easyUi中的一段漂亮代码之将list转换成tree.

    function convert(rows){ function exists(rows, parentId){ for(var i=0; i<rows.length; i++){ if (ro ...

  3. Using Redis as Django's session store and cache backend

    http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/

  4. [SQL]合并字符串

    --带符号合并行列转换 --有表t,其数据如下: /* a b 1 1 1 2 1 3 2 1 2 2 3 1 --如何转换成如下结果: a b 1 1,2,3 2 1,2 3 1 */ drop t ...

  5. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  6. Chapter Configuration

    Chapter Configuration 在Web.config 或App.config的configuration节,插入如下配置: <configuration> …… <co ...

  7. Android 进阶 Fragment 介绍和使用 (一)

    Fragment概述 Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一 ...

  8. python编写接口

  9. 使用 iMacros 来自动化日常的工作

    利用 iMacros 的浏览器附加组件来提高工作效率 介绍 iMacros 这个强大的工具,使用简单的范例演示了如何使用这个工具来完成对于网页的操作,对于大量的具有重复性的工作内容尤其可以提高效率.对 ...

  10. 使用virtualbox安装centos虚拟机,以及VirtualBox无法安装64位Linux CentOS的解决办法

    之前一直用vmware的虚拟机,好吧,其实一直盗版挺不好的,然后想用centos搭点东西,结果在vmare上安装centos总是有些问题,看了人给的建议换用virtualbox,虽然virtualbo ...