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

一开始没理解,以为是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. printf("%*s\n", 1, ""); 使用"printf();" 的格式化输出动态制定等宽度输出。

    #include <stdio.h> #include <string.h> int main() { const char the_text[] = "this i ...

  2. Oracle中常用操作

    查看表中的字段名和类型 SELECT column_name,DATA_TYPE FROM cols WHERE TABLE_NAME=upper('tableName') ORDER BY COLU ...

  3. C# 操作网页标签

    1  元素的 显示与隐藏   C# HTML: <div Id="div1" runat="server" style="display:non ...

  4. 华硕X84L无线驱动查找

    打开官网:http://www.asus.com.cn/ 点击导航栏的服务与支持 产品型号识别http://www.asus.com.cn/support/Article/565/ 我的是:X84L  ...

  5. jquery mobile导致无法修改textarea的高度

    在引用了jquery mobile的js库和样式的页面中,添加textarea标签,会导致无法调整其height属性,不管是用CSS还是JS,最终都会被替换成height=52px 解决办法:在页面中 ...

  6. 【Python】迭代器、生成器、yield单线程异步并发实现详解

    转自http://blog.itpub.net/29018063/viewspace-2079767 大家在学习python开发时可能经常对迭代器.生成器.yield关键字用法有所疑惑,在这篇文章将从 ...

  7. JavaScript学习笔记---入门

    1.JavaScript 语句和 JavaScript 变量都对大小写敏感. 2.重新声明 JavaScript 变量 如果重新声明 JavaScript 变量,该变量的值不会丢失: 在以下两条语句执 ...

  8. Find the Clones(字典树)

    链接:http://poj.org/problem?id=2945 Description Doubleville, a small town in Texas, was attacked by th ...

  9. How to Resize a Datafile (文档 ID 1029252.6)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 and laterInformation in this docu ...

  10. 菜鸟-手把手教你把Acegi应用到实际项目中(2)

    上一篇是基于BasicProcessingFilter的基本认证,这篇我们改用AuthenticationProcessingFilter基于表单的认证方式. 1.authenticationProc ...