Roman Number & Integer

Problem I: 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. 将字符和整数存入map中;

2. 从后往前扫描,如果当前字符代表的值大于后一个字符的值,则用当前的值减去之前累计的值。e.g. "IV" -> 5 - 1 = 4

程序

public class Solution {
    public int romanToInt(String s) {
        if (s == null || s.length() == 0) {
		    return 0;
	    }

    	HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
    	hm.put(‘I‘, 1);
    	hm.put(‘V‘, 5);
    	hm.put(‘X‘, 10);
    	hm.put(‘L‘, 50);
    	hm.put(‘C‘, 100);
    	hm.put(‘D‘, 500);
    	hm.put(‘M‘, 1000);

    	int len = s.length();
    	int sum = hm.get(s.charAt(len - 1));

    	for (int i = len - 2; i >= 0; i--) {
    	    char cur = s.charAt(i);
    	    char aft = s.charAt(i + 1);
    	    if (hm.get(cur) < hm.get(aft)) {
    	        sum -= hm.get(cur);
    	    } else {
    	        sum += hm.get(cur);
    	    }
    	}

    	return sum;
    }
}

Problem II: Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

解决思路

1. 存一个数字到字符的映射表,数字从大到小,包含{1000, 900, 500, 400, 100, 90,
            50, 40, 10, 9, 5, 4, 1};

2. 贪心原则,得到对应的字符串。

程序

public class Solution {
    public String intToRoman(int num) {
        String res = "";
        if (num <= 0) {
			return res;
		}

		int[] nums = {1000, 900, 500, 400, 100, 90,
			50, 40, 10, 9, 5, 4, 1};
		String[] symbols = {"M", "CM", "D", "CD", "C", "XC",
			"L", "XL", "X", "IX", "V", "IV", "I"};

		int i = 0;

		while (num > 0) {
		    int j = num / nums[i];
		    num -= j * nums[i];
		    for ( ; j > 0; j--) {
		        res += symbols[i];
		    }
		    ++i;
		}

		return res;
    }
}
时间: 2024-10-13 03:43:12

Roman Number & Integer的相关文章

Leetcode 题目整理-3 Palindrome Number &amp; Roman to Integer

9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the res

12. Integer to Roman &amp;&amp; 13. Roman to Integer &amp;&amp; 273. Integer to English Words

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

[LeetCode][JavaScript]Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. https://leetcode.com/problems/roman-to-integer/ 罗马数字转阿拉伯数字. 从后往前扫,如果当前的数大于之前的数,加上这个数,反之减去当前的数. 1 /** 2 * @param {string} s 3

Integer to Roman &amp; Roman to Integer

Integer to Roman Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range from 1 to 3999. Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals https://zh.wikipedia.org/wiki/%E7%BD%9

Leetcode 13. Roman to Integer(水)

13. Roman to Integer 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 i

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这几个特殊值(左

leetCode 13. Roman to Integer 字符串

13. Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 补充:罗马数字 罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

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. I

LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则.Wiki了一把,又参考了其它的文档,总结如下: 罗马数字规则: 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 罗马数字中没有"0". 2, 重复次数:一个罗马数字最多重复3次. 3, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上