[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 ...
随机推荐
- MS Sql Server 中主从库的配置和使用介绍(转)
网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库. 主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据 ...
- MyEclipse运行时自动保存
今天第一次用MyEclipse,我发现我的代码明明修改了,但运行结果发现总是修改前的代码结果.后来发现,是代码修改后必须保存,再点运行.这个功能明显不合适,所以需要更改MyEclipse的配置.红框是 ...
- AngularJs在单击提交后显示验证信息.
<!DOCTYPE html> <html ng-app="app"> <head> <meta charset="utf-8& ...
- ubuntu 14.10 安装 zabbix
在ubuntu 14.10 上部署 zabbix 2.x 基本软件包安装 既然是ubuntu系统,当然要用好apt-get神器. 参考教程 URL:http://blog.csdn.net/cloud ...
- java selenium 项目环境搭建(一)
1.使用jdk1.7版本.jdk下载,请再百度输入 jdk 1.7下载,环境配置参考
- 1、程序启动原理和UIApplication【转】
一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就代表一个应用程序. (2)每一个应用都有自己的UIApplica ...
- Oracle中MD5+Base64加密实现
JAVA实现: public static String getMD5(String str) throws Exception { MessageDigest md5 = MessageDige ...
- OpenStack Nova 制作 Windows 镜像
OpenStack Nova 制作 Windows 镜像 windows虚拟机ubuntuimage防火墙云计算 本贴转自http://www.vpsee.com 上次 VPSee 给 OpenS ...
- Android中MenuInflater实例
我们知道,LayoutInflater是用来实例化整个布局文件,而MenuInflater是用来实例化Menu目录下的Menu布局文件的. 传统意义上的菜单定义需要Override Activity的 ...
- Android开发-API指南-常用Intent
Common Intents 英文原文:http://developer.android.com/guide/components/intents-common.html 采集(更新)日期:2014- ...