码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]165 Compare Version Numbers

时间:2015-01-09 19:31:02      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:leetcode

https://oj.leetcode.com/problems/compare-version-numbers/

http://blog.csdn.net/u012243115/article/details/41969181

public class Solution {
    public int compareVersion(String version1, String version2) {
        if (version1 == null || version2 == null)
            return 0; // Invalid input.
            
        // NOTE!!
        // Here is the tricky point.
        String[] v1 = version1.split("\\.");
        String[] v2 = version2.split("\\.");
        
        for (int i = 0 ; i < v1.length || i < v2.length ; i ++)
        {
            int value1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
            int value2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;
            int r = Integer.compare(value1, value2);
            if (r != 0)
                return r;
        }
        return 0;
    }
    
    private int compare(String a, String b)
    {
        return Integer.compare(Integer.parseInt(a), Integer.parseInt(b));
    }
}


[LeetCode]165 Compare Version Numbers

标签:leetcode

原文地址:http://7371901.blog.51cto.com/7361901/1601307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!