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.

给定一个罗马数字,把它转换成一个整数。

把罗马数字字符串转换成字符数组先,如下表,每个数字仅对应一个字符,而且字符不一样。故可从头开始取值进行对应。

The Roman Symbols

The Romans used a special method of showing numbers, based on the following symbols:


1

5

10

50

100

500

1000

I

V

X

L

C

D

M

	public int romanToInt(String s) {
		Map<Character, Integer> romans = new HashMap<Character, Integer>();
		romans.put('I', 1);
		romans.put('V', 5);
		romans.put('X', 10);
		romans.put('L', 50);
		romans.put('C', 100);
		romans.put('D', 500);
		romans.put('M', 1000);
		char[] ch = s.toCharArray();
		int num = 0, val = 0;
		for (int i = 0; i < ch.length; i++) {
			val = romans.get(ch[i]);
			if (i == ch.length - 1 || romans.get(ch[i + 1]) <= val)
				num += val;
			else
				num -= val;
		}
		return num;
	}

Reference:http://www.mathsisfun.com/roman-numerals.html

LeetCode——Roman to Integer

时间: 2024-10-11 08:44:18

LeetCode——Roman to Integer的相关文章

Leetcode | Roman to Integer &amp; Integer to Roman

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 class Solution { 2 public: 3 int romanToInt(string s) { 4 if (s.empty()) return 0; 5 int dict[100]; 6 d

LeetCode Roman to Integer 罗马字符转数字 解题报告

https://oj.leetcode.com/problems/roman-to-integer/ Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 把一个给定的罗马字符转为数字.首先要了解罗马字符表示的规则. 一,羅馬數字共有7個,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 二,在

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. 要把罗马数字转换为整数, 罗马数字自行百度 code: class Solution { public: int romanToInt(string s) { map<char,int> Roman; Roman['I'] = 1; Roman['V'] = 5; Roma

[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个字母顺序来的,如何确定大小.解决办法一:写

leetcode Roman to Integer python

class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ numerals = { "M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I"

Roman to Integer [LeetCode 13]

1- 问题描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 2- 罗马数字[1] 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗马数字中没有“0”,与进位制无关.一般认为罗马数字只用来记数,而不作演算. 重复数次:一个

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之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, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上

[LeetCode][Python]Roman to Integer

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/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.===Comments by Daba