关于java:LeetCode刷题No13罗马数字转整数一

题目起源:力扣(LeetCode)
链接:https://leetcode-cn.com/probl…
罗马数字蕴含以下七种字符: I, V, X, L,C,D 和 M。

字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。

通常状况下,罗马数字中小的数字在大的数字的左边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的右边,所示意的数等于大数 5 减小数 1 失去的数值 4 。同样地,数字 9 示意为 IX。这个非凡的规定只实用于以下六种状况:

  • I 能够放在 V (5) 和 X (10) 的右边,来示意 4 和 9。
  • X 能够放在 L (50) 和 C (100) 的右边,来示意 40 和 90。 
  • C 能够放在 D (500) 和 M (1000) 的右边,来示意 400 和 900。
  • 给定一个罗马数字,将其转换成整数。输出确保在 1 到 3999 的范畴内

    算法思维:
    从头开始遍历,顺次累加。
    思考到罗马字符的特点,从后开始顺次判断绝对更好一些
    代码展现

class Solution {
    public int romanToInt(String s) {
        //从头遍历
        int res = 0;
        char cur, nxt;
        for(int i = 0; i < s.length(); i++){
            cur = s.charAt(i);
            nxt = i + 1 < s.length() ? s.charAt(i + 1) : cur;
            if(cur == 'M'){
                res += 1000;
            }else if(cur == 'C' && nxt == 'M'){
                res += 900;
                i++;
            }else if(cur == 'D'){
                res += 500;
            }else if(cur == 'C' && nxt == 'D'){
                res += 400;
                i++;
            }else if(cur == 'C'){
                res += 100;
            }else if(cur == 'X' && nxt == 'C'){
                res += 90;
                i++;
            }else if(cur == 'L'){
                res += 50;
            }else if(cur == 'X' && nxt == 'L'){
                res += 40;
                i++;
            }else if(cur == 'X'){
                res += 10;
            }else if(cur == 'I' && nxt == 'X'){
                res += 9;
                i++;
            }else if(cur == 'V'){
                res += 5;
            }else if(cur == 'I' && nxt == 'V'){
                res += 4;
                i++;
            }else{
                res += 1;
            }
        }
        return res;
    }
}
class Solution {
    public int romanToInt(String s) {
        //从后开始
        char[] chs=s.toCharArray();
        int res=0;  //存储最终后果

        //依照字符程序遍历
        for(int i=0;i<chs.length-1;i++){
            //以后字符对应的数字,小于左边的数字时,减去它
            if(helper(chs[i]) < helper(chs[i+1])){
                res-=helper(chs[i]);
            }else{
                res+=helper(chs[i]);
            }
        }

        //加上最初一个字符对应的数字
        res+=helper(chs[chs.length-1]);
        return res;
    }

    public int helper(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;
            default : return 0;
        }
    }
}

心得体会
对于HAshMap这种,本人齐全了解不了,想不分明须要增强。
对于判断条件,能够退出逻辑关系词比方&&来节俭语句。
记录下主函数

  public static String stringToString(String input) {
        if (input == null) {
            return "null";
        }
        return Json.value(input).toString();
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            String s = stringToString(line);
            
            int ret = new Solution().romanToInt(s);
            
            String out = String.valueOf(ret);
            
            System.out.print(out);
        }
    }

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理