题目描述:

Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the . character.
The . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

解题思路:

以小数点分开,逐个比较。

代码如下:

public class Solution {
public int compareVersion(String version1, String version2) {
String[] levels1 = version1.split("\\.");
String[] levels2 = version2.split("\\.");
int length = Math.max(levels1.length, levels2.length);
for(int i = 0; i < length; i++){
Integer v1 = i < levels1.length ? Integer.parseInt(levels1[i]) : 0;
Integer v2 = i < levels2.length ? Integer.parseInt(levels2[i]) : 0;
int compare = v1.compareTo(v2);
if(compare != 0)
return compare;
}
return 0;
}
}

  

Java [Leetcode 165]Compare Version Numbers的更多相关文章

  1. Java for LeetCode 165 Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  2. ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  3. [LeetCode] 165. Compare Version Numbers 比较版本数

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  4. Leetcode 165 Compare Version Numbers

    题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...

  5. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  6. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

  7. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  8. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  9. 【LeetCode】165 - Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. HDU 4101 Ali and Baba

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4101 一看之下以为是博弈,后来分析才知道只是搜索题. 首先,我们需要从值为-1的位置由内向外搜索一次, ...

  2. 清除HTML中的特殊字符

    /// <summary>        /// 清楚HTML中的特殊字符        /// </summary>        /// <param name=&q ...

  3. Eclipse和intellij idea 快捷键对比

    Eclipse和intellij idea 快捷键对比

  4. MySQL主从关系设置(转)

    来源:LAMP兄弟连 作者:李恺 http://***/php/bencandy.php?fid=70&id=635 要做MySQL主从关系的设置,那么就得有两台MySQL主机.所以在开始之前 ...

  5. LA 4329

    第一次敲树状数组  因为一个小错误 wa了 n 多遍  终于ac  太不容易了 /*********************************************************** ...

  6. uva 10131

    DP 先对大象体重排序   然后寻找智力的最长升序子列  输出路径.... #include <iostream> #include <cstring> #include &l ...

  7. ios iap 购买总是提示继续的解决方案

    原地址:http://blog.csdn.net/kafeidev/article/details/8619984 ========================================== ...

  8. Lua 的数据结构

    1. Arrays: 注意 #(data), # 加上 table名字 == size of data = {}; , do --行 , do --列 data[(y-)*+x] = (y-)*+x; ...

  9. Oracle调优总结(经典实践 重要)

    转载:http://langgufu.iteye.com/blog/1974211 Problem Description:1.每个表的结构及主键索引情况2.每个表的count(*)记录是多少3.对于 ...

  10. 移动开发之meta篇

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable= ...