LeetCode 13 Roman to Integer(罗马数到整型数)

简介:

翻译

给定一个罗马数字,将其转换到整型数值。

输入被保证在1到3999之间。

原文

Given a roman numeral, convert it to an integer.

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

一开始就没有构思好,虽然按上一题的套路可以走下去,但结果就是像我下面这样……代码凌乱……

public class Solution
{
    public int RomanToInt(string s)
    {
        int result = 0;
        Type R = typeof(Roman);
        string first, second;
        if (s.Length > 1)
        {
            first = s.Substring(0, 1); second = s.Substring(0, 2);
        }
        else
        {
            first = s.Substring(0, 1); second = "";
        }
        foreach (var r in Enum.GetNames(R).Reverse())
        {
            while ((r.Length == 1 && first == r) || (r.Length == 2 && second == r))
            {
                result += int.Parse(Enum.Format(R, Enum.Parse(R, r), "d"));
                int lenR = r.Length, lenS = s.Length;
                if (lenS - lenR < 1)
                    s = "";
                else
                    s = s.Substring(lenR, lenS - lenR);

                if (s.Length > 1)
                {
                    first = s.Substring(0, 1); second = s.Substring(0, 2);
                }
                else if (s.Length == 1)
                {
                    first = s.Substring(0, 1); second = "";
                }
                else
                {
                    first = ""; second = "";
                }
            }
        }
        return result;
    }

}

public enum Roman
{
    M = 1000,
    CM = 900,
    D = 500,
    CD = 400,
    C = 100,
    XC = 90,
    L = 50,
    XL = 40,
    X = 10,
    IX = 9,
    V = 5,
    IV = 4,
    I = 1
};

虽然吧,可以运行……但是效率也太低了,简直不忍直视……于是还是像其他大神学习学习……

下面这段代码深深的打动了我……数组作为数组的索引……我最不常用的用法了……

class Solution {
public:
    int romanToInt(string s) {
        unordered_map<char, int> map = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
        int ret = 0;
        for (int idx = 0; idx < s.size(); ++idx) {
            if ((idx < s.size()-1) && (map[s[idx]] < map[s[idx+1]])) {
                ret -= map[s[idx]];
            }
            else {
                ret += map[s[idx]];
            }
        }
        return ret;
    }
};

这个算法呢,充分利用了罗马数字两个数字前后顺序的关系,也就是说如果是I在V前面,也就是IV,它代表4,反之代表6。再搭配C++的unordered_map,可以巧妙的通过数组来进行判断而达到对ret或加或减的目的。

我需要好好体会了……

目录
相关文章
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 166. 分数到小数 算法解析
☆打卡算法☆LeetCode 166. 分数到小数 算法解析
LeetCode 343. Integer Break
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
252 0
LeetCode 343. Integer Break
|
机器学习/深度学习
LeetCode 397. Integer Replacement
给定一个正整数 n,你可以做如下操作: 1. 如果 n 是偶数,则用 n / 2替换 n。 2. 如果 n 是奇数,则可以用 n + 1或n - 1替换 n。 n 变为 1 所需的最小替换次数是多少?
351 0
LeetCode之Reverse Integer
LeetCode之Reverse Integer
295 0
|
Java
[LeetCode] Roman to Integer 罗马数字转化成整数
链接:https://leetcode.com/problems/roman-to-integer/#/description难度:Easy题目:13. Roman to Integer Given a roman numeral, convert it to an integer.
1020 0
|
Java 编译器
[LeetCode]Reverse Integer题解
题目链接:7. Reverse Integer 难度:Easy Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 N...
967 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
653 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行

热门文章

最新文章