Leetcode_13_Roman to Integer
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41486885
通过本文你可能学到的知识如下:
(1)理解本题的解题思路,在以后类似的场景中,如果没有想到比较好的方法,可以考虑使用本文的方法,虽然效率不是特别高。
(2)能够对字符串的截取和HashMap相关操作有所学习。
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解题思路如下:
(1)这道题其实不难,可以用switch进行条件判断,并依据返回值累加求得结果。本文使用的是HashMap。
(2)将罗马数字和对应的整数存储在HashMap中。由于题目限制条件为1—3999,所以需要存储的罗马数字有:1-9、10-100、100-1000、1000-3000,数量其实
不多。
(3)对于给定的罗马数字字符串,首先,对其长度进行判断,如果长度不为0,则继续。其次,我们发现罗马数字中有很对数字之间具有包含关系,例如III包含II和
I,所以,对于给定的罗马数字字符串,需要判断其子串在Map中对应的值是否为空。我们首先截取第一个字符,判断其在Map中的值是否为空,如果不为空,继续截
取到第二个字符,如果这两个字符在Map中值不为空,我们继续截取到第三个字符,如果这三个字符在Map中值不为空,继续下去......,直到截取到的字符在Map中对
应的值为空,那么将最后添加进去之前的字符对应在Map中的值存储起来,以此类推,直到字符串中所有字符都涉及到截取操作,最后得到的值即为对应的整数的值。
算法实现代码如下所示(PS:本人技术有限,目前还不能写出高效的算法,大家有好的算法希望能够分享,谢谢)
public int romanToInt(String s) {
Map<String, Integer> maps = new HashMap<String, Integer>();
maps.put("I", 1);
maps.put("II", 2);
maps.put("III", 3);
maps.put("IV", 4);
maps.put("V", 5);
maps.put("VI", 6);
maps.put("VII", 7);
maps.put("VIII", 8);
maps.put("IX", 9);
maps.put("X", 10);
maps.put("XX", 20);
maps.put("XXX", 30);
maps.put("XL", 40);
maps.put("L", 50);
maps.put("LX", 60);
maps.put("LXX", 70);
maps.put("LXXX", 80);
maps.put("XC", 90);
maps.put("C", 100);
maps.put("CC", 200);
maps.put("CCC", 300);
maps.put("CD", 400);
maps.put("D", 500);
maps.put("DC", 600);
maps.put("DCC", 700);
maps.put("DCCC", 800);
maps.put("CM", 900);
maps.put("M", 1000);
maps.put("MM", 2000);
maps.put("MMM", 3000);
if (s.length() == 0)
return -1;
int count = 0;
int flag = 0;
for (int i = 0; i < s.length(); i++) {
while (flag < s.length()
&& maps.get(s.substring(i, flag + 1)) != null) {
flag++;
}
count = count + maps.get(s.substring(i, flag));
i = flag - 1;
}
return count;
}
Leetcode_13_Roman to Integer的更多相关文章
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [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] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. 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. ...
随机推荐
- cannot open file "cxcore.lib"
运行例子程序的时候总是出现连接错误:LINK : fatal error LNK1104: cannot open file "cxcore.lib". 在VC选项里把C:\Pro ...
- python while条件和if判断的总练习
输出123456 89的数字 num =1 while num < 11: if num == 7: pass else: print(num) num = num + 1 输出1-100的奇数 ...
- 多表insert操作详解
--1.无条件的多表insert all ; ; ; --没有条件,向多个目标表全量插入,必须有all insert all --不指定emp_1后面的列,也不指定values,那么emp_1中的所有 ...
- PHP $_POST 变量
$_POST 变量 预定义的 $_POST 变量用于收集来自 method="post" 的表单中的值. 从带有 POST 方法的表单发送的信息,对任何人都是不可见的(不会显示在浏 ...
- MyBatis 查询映射自定义枚举
背景 MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用 ...
- sublime text package control 被墙的解决办法
似乎没有办法 只能碰运气, 时好时坏. 或者手动安装 趁着好的时候, 下载离线包 https://packagecontrol.io/Package%20Control.sublime-package ...
- Afianl加载网络图片(续)
上一篇已经讲了如何利用Afianl加载网络图片和下载文件,这篇文章将继续讲解使用Afinal加载网络图片的使用,主要结合listview的使用: 看效果图: listview在滑动过程中没用明显卡顿, ...
- Java基本语法-----java数组(一维数组二维数组)
嘿嘿!你们懂的,又是图片,委屈大家了. java数组(一维数组二维数组) [正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个"顶"字,你就 ...
- Hadoop与分布式数据处理 Spark VS Hadoop有哪些异同点?
Spark是一个开源的通用并行分布式计算框架,由加州大学伯克利分校的AMP实验室开发,支持内存计算.多迭代批量处理.即席查询.流处理和图计算等多种范式.Spark内存计算框架适合各种迭代算法和交互式数 ...
- 开源协议介绍(GPL,LGPL,BSD,MIT,Apache)
http://blog.csdn.net/zhulinu/article/details/7419068 什么是许可协议? 什么是许可,当你为你的产品签发许可,你是在出让自己的权利,不过,你仍然拥 ...