本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  2. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  3. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  4. [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 ...

  5. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  7. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. 让互联网更快,Server Push 特性及开启方式详解

    过去 Nginx 并不支持 HTTP/2 的 Server Push 特性,幸运的是 Nginx 1.13.9 已支持该特性,详情介绍请移步 Nginx 官方博客. Server Push 这个特性是 ...

  2. Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》

    转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...

  3. Rails多路径调用相同方法原路返回的方法

    有时候可能有多条path到达同一个method,此时,我们希望在该方法完成后自动转到之前进入的path中去,其实实现起来非常简单,只需要实现如下两个方法: def redirect_back_or(d ...

  4. Docker: How to enable/disable HTTP Proxy in Toolbox

     1. docker-machine ssh default 2. sudo vi /var/lib/boot2docker/profile 3. # replace with your offi ...

  5. 在android系统上写C语言程序--开机启动该程序不进入安卓系统

    今天要写的这篇博文意义重大,也是网上很少有的,这是在我工作中学会的一项技术,当然,它也是由简单的问题组合而来的.如何在安卓中写C语言程序,调试安卓驱动,测试程序的的一项重要技能,下面我就不说废话了,直 ...

  6. clang-format中文出错

    clang-format中文出错(金庆的专栏)VS2015 Community + clang-format(Visual Studio plugin installer, based on SVN ...

  7. Android捕获全局异常

    Android捕获全局异常 程序避免不了出现bug,导致程序崩溃,为了尽量不影响用户体验,可以全局捕获异常 效果图 异常捕获处理前 异常捕获处理后(将程序重新启动) 捕获异常的工具类 package ...

  8. 基于Web在线考试系统的设计与实现

    这是一个课程设计的文档,源码及文档数据库我都修改过了,貌似这里复制过来的时候图片不能贴出,下载地址:http://download.csdn.net/detail/sdksdk0/9361973   ...

  9. [nginx]统计文件下载是否完整思路(flask)

    有一个需求是统计文件是否被用户完整下载,因为是web应用,用js没有找到实现方案,于是搜索下nginx的实现方案,把简单的探索过程记录下. 实验一 最原始的思路,查看日志,下载了一个文件之后我们看日志 ...

  10. lucene内存索引库、分词器

    内存索引库 特点 在内存中开辟一块空间,专门为索引库存放.这样有以下几个特征: 1)    因为索引库在内存中,所以访问速度更快. 2)    在程序退出时,索引库中的文件也相应的消失了. 3)    ...