[leetcode]_Roman to Integer
题目:给定一个罗马数字串,转换为一个整数。
一开始没理解,以为是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的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- 【一天一道LeetCode】#12 Integer to Roman
一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- LeetCode题目_Reverse Integer
最近在LeetCode上做题,写点东西记录一下,虽然自己做的都是些很水的题目,但是重在练手. 题号7:Reverse Integer,题目描述: Reverse digits of an intege ...
随机推荐
- [Flex] PopUpButton系列 —— 打开和关闭弹出菜单
<?xml version="1.0" encoding="utf-8"?><!--响应打开和关闭弹出菜单的例子 PopUpButtonOpe ...
- 用SPCOMM 在 Delphi中实现串口通讯 转
用Delphi 实现串口通讯,常用的几种方法为:使用控件如MSCOMM和SPCOMM,使用API函数或者在Delphi 中调用其它串口通讯程序.利用API编写串口通信程序较为复杂,需要掌握大量通信 ...
- 微信红包签名算法 C#代码实现
string stringA = "appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=100001 ...
- 如何使weblogic11g类似weblogic923一样统一使用一个boot.properties文件
如何使weblogic11g类似weblogic923一样 统一使用一个boot.properties文件 1.在weblogic域下创建文件boot.properties输入用户密码例如:usern ...
- [SQL] 要查询9 月份的数据中的任意时间段,可能是一个月的,也可能是1日到15日的
SELECT * FROM [表名] WHERE datediff(month,[列名],
- [ACDream 1430]SETI 后缀数组
题目链接:http://acdream.info/problem?pid=1430 题目大意:给你一个长度不超过10000的字符串,问你出现过两次或两次以上的不重叠的子串有多少个. 后缀数组计算出he ...
- Altium designer 原理图库快速创建
Altium designer 原理图库快速创建,原来都没发现用这个功能,最近查了一下很好用,就是通过Excel编写管脚名称再直接导入就可以了,很方便的. 1.首先在Excel创建填好对应管脚名称. ...
- cwRsync 配置文件详解
GLOBAL PARAMETERS(全局参数) The first parameters in the file (before a [module] header) are the global p ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem I
Problem I Interesting Calculator There is an interesting calculator. It has 3 rows of button. Row 1: ...
- maven的版本管理笔记
1. 版本管理 (1) 快照版本对应了项目的开发过程,往往对应了很长的时间:发布版本对应了项目的发布,因此仅仅代表某个时刻项目的状态. (2) 理想的发布版本应当对应项目某个时刻比较稳定的状态,包括源 ...