首页 > 其他 > 详细

7-1日刷题

时间:2015-07-01 00:57:00      阅读:381      评论:0      收藏:0      [点我收藏+]

Roman to Integer

技术分享
    public int romanToInt(String s) {
        //数字:Ⅰ(1)Ⅴ(5)Ⅹ(10)L(50)C(100)D(500)M(1000) 
        HashMap<Character, Integer> map = new HashMap<>();
        map.put(‘I‘,1);
        map.put(‘V‘,5);
        map.put(‘X‘,10);
        map.put(‘L‘,50);
        map.put(‘C‘,100);
        map.put(‘D‘,500);
        map.put(‘M‘,1000);
        int cur = map.get(s.charAt(s.length() - 1));
        int result = cur;
        for (int i = s.length() - 2; i >= 0; --i) {
            int pre = map.get(s.charAt(i));
            if (cur > pre) {
                result -= pre;
            } else {
                result += pre;
            }
            cur = pre;
        }
        return result;
    }
View Code

 

7-1日刷题

原文:http://www.cnblogs.com/fripside/p/4612168.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!