[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


013.Roman_to_Integer (Easy)

链接

题目:https://oj.leetcode.com/problems/roman-to-integer/

代码(github):https://github.com/illuz/leetcode

题意

把罗马数转为十进制。

分析

012. Integer to Roman (Medium) 一样,只要知道转化规则就行了。

代码

C++:

class Solution {
private:
	int val[255];
	void init() {
		val['I'] = 1; val['V'] = 5; val['X'] = 10; val['L'] = 50;
		val['C'] = 100; val['D'] = 500; val['M'] = 1000;
	}
public:
    int romanToInt(string s) {
		init();
		int ret = 0;
		for (int i = 0; i < s.size(); i++) {
			if (i > 0 && val[s[i]] > val[s[i - 1]]) {
				ret += val[s[i]] - 2 * val[s[i - 1]];
			} else {
				ret += val[s[i]];
			}
		}
		return ret;
    }
};

Java:

public class Solution {
    private int[] val = new int[255];
    private void init() {
        val['I'] = 1; val['V'] = 5; val['X'] = 10; val['L'] = 50;
        val['C'] = 100; val['D'] = 500; val['M'] = 1000;
    }

    public int romanToInt(String s) {
        init();
        int ret = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i > 0 && val[s.charAt(i)] > val[s.charAt(i - 1)]) {
                ret += val[s.charAt(i)] - 2 * val[s.charAt(i - 1)];
            } else {
                ret += val[s.charAt(i)];
            }
        }
        return ret;
    }
}

Python:

class Solution:
    # @return an integer
    def romanToInt(self, s):
        val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
        ret = 0
        for i in range(len(s)):
            if i > 0 and val[s[i]] > val[s[i - 1]]:
                ret += val[s[i]] - 2 * val[s[i - 1]]
            else:
                ret += val[s[i]]
        return ret
时间: 2024-12-09 19:25:03

[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)的相关文章

[LeetCode] 008. String to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 008.String_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/string-to-integer-atoi/ 代码(github):https://github.com/illuz/leetcode 题意: 将一个字符串转化为 int 型.

LeetCode 13 Roman to Integer (C,C++,Java,Python)

Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Solution: 时间复杂度O(n) 题目大意: 与12题相反,给一个罗马数字,要求转化为十进制数字 解题思路: Java源代码(用时749ms): public class Solution { public int romanToInt(String s) {

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] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

[LeetCode] 014. Longest Common Prefix (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 014.Longest_Common_Prefix (Easy) 链接: 题目:https://oj.leetcode.com/problems/longest-common-prefix/ 代码(github):https://github.com/illuz/leetcode 题意: 求多个字符串的最长公共前缀

[LeetCode OJ] 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 map<char,int> conversion; 5 conversion.insert(make_pair('

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] 020. Valid Parentheses (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 020.Valid_Parentheses (Easy) 链接: 题目:https://oj.leetcode.com/problems/valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个括号字符串是否是有效的. 分析:

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 006.ZigZag_Conversion (Easy) 链接: 题目:https://oj.leetcode.com/problems/zigzag-conversion/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个字符串按横写的折线排列. 分析: 直