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;
}
时间: 2024-08-02 02:46:13

Roman to Integer——罗马数字转变算法的相关文章

[Leetcode] Roman to integer 罗马数字转成整数

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 思路:有关罗马数字的相关知识可见博客Integer to roman.从左往右遍历字符串,比较当前为和下一位的值,若是当前数字比下一位大,或者当当前数字为最后时,则加上当前数字,否则减去当前数字.这就遇到一个问题,罗马数字的字符串不是按26个字母顺序来的,如何确定大小.解决办法一:写

13. Roman to Integer 罗马数字转化为阿拉伯数字(indexOf ()和 toCharArray())easy

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which i

[LeetCode]13. Roman to Integer罗马数字转整数

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which i

[LintCode] Roman to Integer 罗马数字转化成整数

Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Have you met this question in a real interview? Yes Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals htt

LeetCode 13.Roman to Integer 罗马数字转阿拉伯数字

对于罗马数字转阿拉伯数字切入点有两个: 1.小数字出现在大数字前面只能使用一个(例如IV正确,IIV就是错误的) 2.除了情况1之外直接使用累加就ok 1 public int romanToInt(String s){ 2 if(s.length() < 1){ 3 return 0; 4 } 5 int result = 0; 6 for(int i=0;i<s.length();i++){ 7 result += getRomanValue(s.charAt(i)); 8 if(i<

13 Roman to Integer(罗马数字转int Easy)

题目意思:罗马数字转int 思路:字符串从最后一位开始读,IV:+5-1 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 map<char,int> mymap; 5 mymap['I']=1; 6 mymap['V']=5; 7 mymap['X']=10; 8 mymap['L']=50; 9 mymap['C']=100; 10 mymap['D']=500; 11 mymap['M']=1000; 12 int a

58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字,转换为阿拉伯数字.本题只考虑3999以内的数. 罗马数字有如下符号: Ⅰ(1)Ⅴ(5)Ⅹ(10)L(50)C(100)D(500)M(1000) 计数规则: (1).若干相同数字连写表示的数是这些罗马数字的和,如III=3: (2).小数字在大数字前面表示的数是用大数字减去小数字,如IV=4: (3

Leetcode#13. Roman to Integer(罗马数字转整数)

题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数

LeetCode 013 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [题意] 把罗马数转换为整数 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 大体思路是每个罗马字母对应的值相加即可, 但需要处理900, 400, 90, 40, 9, 4这几个特殊值(左