LeetCode刷题2

in 力扣 with 123 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 / Cancel Reply
  1. I wanted to make a small message in order to express gratitude to you for those superb suggestions you are posting on this website. My time intensive internet lookup has finally been recognized with reliable content to write about with my close friends. I 'd admit that most of us website visitors actually are extremely blessed to live in a remarkable website with many perfect people with interesting concepts. I feel truly lucky to have used your entire site and look forward to so many more cool moments reading here. Thank you again for everything.

    Reply
  2. I would like to show thanks to you for rescuing me from such a setting. Right after researching through the the web and coming across notions which were not pleasant, I assumed my life was gone. Existing devoid of the answers to the difficulties you've resolved as a result of your article content is a serious case, as well as the ones which might have adversely affected my entire career if I hadn't come across your web page. Your own expertise and kindness in playing with all the pieces was valuable. I am not sure what I would have done if I hadn't come across such a step like this. I'm able to at this moment look forward to my future. Thanks so much for the skilled and result oriented guide. I won't hesitate to propose your web sites to anyone who requires guidelines on this subject.

    Reply
  3. norvasc price purchase prilosec for sale order prilosec 10mg online cheap

    Reply
  4. nifedipine 30mg over the counter buy adalat tablets purchase allegra sale

    Reply
  5. Thank you a lot for giving everyone an extraordinarily wonderful opportunity to read in detail from here. It can be so superb and stuffed with a good time for me and my office co-workers to visit the blog at least three times in one week to study the new secrets you have. And of course, I am also actually astounded with your outstanding tactics you serve. Some 2 points in this posting are rather the very best I have ever had.

    Reply
  6. cheap olumiant buy glycomet online cheap cost lipitor

    Reply
  7. buy albuterol 100 mcg pills buy proventil 100mcg pill buy phenazopyridine 200 mg sale

    Reply
  8. Thanks for your entire hard work on this web site. Gloria really loves working on investigations and it's easy to see why. A lot of people know all concerning the powerful manner you offer worthwhile steps through your web blog and as well as strongly encourage contribution from other individuals about this situation then our own child is certainly learning a lot of things. Enjoy the rest of the year. You're carrying out a glorious job.

    Reply
  9. I'm just writing to make you be aware of what a fantastic experience my cousin's girl undergone checking your webblog. She figured out plenty of pieces, with the inclusion of what it's like to have an incredible teaching heart to make most people quite simply comprehend certain extremely tough subject areas. You really surpassed our desires. I appreciate you for supplying such interesting, trusted, informative as well as unique guidance on your topic to Julie.

    Reply
  10. naprosyn oral oral naprosyn 500mg purchase prevacid without prescription

    Reply