本文是在学习中的总结,欢迎转载但请注明出处: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. pentaho cde 自定义复选下拉框 checkbox select

    pentaho  自带的component 虽多,但是当用户需要在一个表格中查看多个组别的数据时,pentaho自带的单选框就不能实现了,所以复选下拉框势在必行,实现效果如下: 实现原理是借用了jqu ...

  2. 关于go语言的通道

    1.记一次gorountine导致的泄漏 在项目中使用https://github.com/deckarep/golang-set这个三方包造成了gorountine泄漏.先来看一下这个包的迭代器设置 ...

  3. ACM Bee

    In Africa there is a very special species of bee. Every year, the female bees of such species give b ...

  4. Dubbo框架应用之(二)--服务治理

    Dubbo服务治理全貌图 当我们现有ITOO平台系统的业务随着用户的逐渐增大,设计的业务越来越广,系统会异常的复杂,在大规模服务之前,我们可以采用的是RMI或Hessian等工具,暴露和引用远程服务, ...

  5. 激活第一个CPU

    回到start_kernel,559行,boot_cpu_init函数,跟start_kernel位于同一文件: 494static void __init boot_cpu_init(void) 4 ...

  6. HA机制下的Hadoop配置

    [版权申明:本文系作者原创,转载请注明出处] 文章出处:http://www.cnblogs.com/sdksdk0/p/5585355.html 作者: 朱培    ID:sdksdk0 ----- ...

  7. 微信开发之使用java获取签名signature(贴源码,附工程)

    一.前言 微信接口调用验证最终需要用到的三个参数noncestr.timestamp.signature: 接下来将会给出获取这三个参数的详细代码 本文的环境eclipse + maven 本文使用到 ...

  8. Android中ViewFlipper的使用详解

    说到android的左右滑动效果我们可以说是在每个应用上面都可以看到这样的效果,不管是微博,还是QQ等. 实现左右滑动的方式很多,有ViewPager(不过这个和需要android-support-v ...

  9. ActiveMQ安装配置及实例

    本文可作为吴水成老师,dubbo课程第21节的学习笔记. ActiveMQ的介绍及功能 参考百度 ActiveMQ的下载 https://activemq.apache.org/activemq-51 ...

  10. Android View框架总结(八)ViewGroup事件分发机制

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52298780 上篇分析了View的事件分发流程,留了一个问题:如果上 ...