LeetCode刷题2

in 力扣 with 121 comments

题目描述

罗马数字转整数,包含以下七种字符: I, V, X, L,C,D 和 M。分别对应1,5,10,50,100,500,1000。

给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。

思路
将每一个对应罗马数字转换成数字,一个一个加起来。当小数字在大数字前时,需要减法。
eg:罗马数字IV,就是V-I,即5-1=4,其他正常叠加

难点

数字转换
方法
使用数组,给每一个罗马数字对应下标,每个下标在数组中对应值。
int romanToInt(char * s){
    int num[26];// 优点在于利用ASCII码,数组空间尽可能小,也更能理解
    num['I' - 'A'] = 1;
    num['V' - 'A'] = 5;
    num['X' - 'A'] = 10;
    num['L' - 'A'] = 50;
    num['C' - 'A'] = 100;
    num['D' - 'A'] = 500;
    num['M' - 'A'] = 1000;
    int result = 0, len = strlen(s);
    for(int i = 0; i < len; i++){
        int value = num[s[i] - 'A'];
        if(i < len-1 && value < num[s[i+1] - 'A']){
            result -= value;
        }else{
            result += value;
        }
    }
    return result;
}

java版本(题解中一个很好的思路)

该方法避免了在数字相加时进行判断,直接++
class Solution {
    public int romanToInt(String s) {
        // 优点在于将6种较为特殊的情况提前转化,在switch中进行判断即可,避免了小数字在大数字前的判断,且减少了switch语句判断的次数。
        // 缺点在于当特例足够多时,会变得臃肿,不及+-判断精简
        s = s.replace("IV","a");
        s = s.replace("IX","b");
        s = s.replace("XL","c");
        s = s.replace("XC","d");
        s = s.replace("CD","e");
        s = s.replace("CM","f");

        int result = 0;
        for(int i = 0;i < s.length(); i++){
            result += getValue(s.charAt(i));
        }
        return result;
    }

    private int getValue(char c) {
        switch(c) {
            case 'I' : return 1;
            case 'V' : return 5;
            case 'X' : return 10;
            case 'L' : return 50;
            case 'C' : return 100;
            case 'D' : return 500;
            case 'M' : return 1000;
            case 'a' : return 4;
            case 'b' : return 9;
            case 'c' : return 40;
            case 'd' : return 90;
            case 'e' : return 400;
            case 'f' : return 900;
        }
        return 0;
    }
}

吐槽一下个人代码问题

  1. 习惯于return 0,导致运行结果不符合预期,多return了一个0
  2. 大小写问题,V和v的问题,导致ASCII码变化,计算结果不符合预期
Responses
  1. I just wanted to thank you for the fast service. in addition to they look great. I received them a day earlier than expected. much I will definitely continue to buy from this site. blue jays I will recommend this site to my friends. Thanks!
    cheap retro jordans https://www.realjordansretro.com/

    Reply
  2. I just wanted to thank you for the fast service. or alternatively they look great. I received them a day earlier than expected. for instance I will definitely continue to buy from this site. direction I will recommend this site to my friends. Thanks!
    cheap louis vuitton handbags https://www.cheapreallouisvuitton.com/

    Reply
  3. I just wanted to thank you for the fast service. quite possibly they look great. I received them a day earlier than expected. since the I will definitely continue to buy from this site. you decide I will recommend this site to my friends. Thanks!
    cheap real jordans https://www.cheaprealjordan.com/

    Reply
  4. I just wanted to thank you for the fast service. or perhaps even they look great. I received them a day earlier than expected. such as the I will definitely continue to buy from this site. in either case I will recommend this site to my friends. Thanks!
    cheap jordan shoes https://www.realcheapretrojordanshoes.com/

    Reply
  5. |There are people who believe that fashion just means clothing. These people fail to understand that bad hair can very easily ruin a great outfit. Purchase products that suit the type of hair that you have, and invest a few extra minutes in the morning to make sure your hair looks great.

    Reply
  6. |Wearing white after the end of summer used to be considered a huge fashion faux pas. Wear what ever colors you prefer, so long as they are flattering. If you feel and look your best wearing white, wear it, but do make sure the fabric is appropriate for the season. Don't let anyone tell you otherwise.

    Reply
  7. {When wearing sheer clothes, make sure the sheer parts are in the right areas. You never want to wear something that is see-through in your private areas, as this gives off the appearance of being trashy and not classy at all.

    Reply
  8. |One thing you are going to want to do is always keep an eye open for changes in style. Styles are constantly changing, and you can find out what is new by looking at fashion magazines every now and then. They are likely going to showcase the new trends first.

    Reply
  9. |Keep up with the latest styles. Styles are constantly changing, and you can find out what is new by looking at fashion magazines every now and then. They typically display news trends in style first.

    Reply
  10. }{When you want to look slimmer, avoid stripes which run horizontally. These stripes will give the illusion of widening and this is not the look you want to achieve. Instead, wear a vertical stripe pattern that will make you look tall instead of wide.

    Reply