题目:

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

思路:

  • 题意:比较两个版本号字符串的大小
  • 把字符串用split转化为数组,注意split(\.),然后转化为整数数组,遍历比较。注意如果版本号后面都是零的情况

代码:

public class Solution {
      public int compareVersion(String version1, String version2) {
        String[] v1,v2;
        if(version1.indexOf(".") == -1){
            v1 = new String[1];
            v1[0] = version1;
        }else{
             v1 = new String[version1.split("\\.").length];
             v1 = version1.split("\\.");
        }
        if(version2.indexOf(".") == -1){
            v2 = new String[1];
            v2[0] = version2;
        }else{
            v2 = new String[version2.split("\\.").length];
            v2 = version2.split("\\.");
        }
        int[] array1 = sToInt(v1);
        int[] array2 = sToInt(v2);
        int nn = Math.min(array1.length,array2.length);
        for(int a = 0;a < nn;a++){
            if(array1[a] > array2[a]){
                return 1;
            }else if(array1[a] < array2[a]){
                return -1;
            }
        }
        if(array1.length > array2.length){
            for(int k = nn; k < array1.length;k++){
                if(array1[k] != 0){
                    return 1;
                }
            }
            return 0;
        }else if(array1.length < array2.length){
            for(int m = nn;m < array2.length;m++){
                if(array2[m] != 0){
                    return -1;
                }
            }
            return 0;
        }
        return 0;
    }
    public int[] sToInt(String[] ss){
        int n = ss.length;
        int[] result = new int[n];
        for(int i = 0;i < n;i++){
            try{
                result[i] = Integer.parseInt(ss[i]);
            }catch(Exception e){

            }
        }
        return result;
    }
}

LeetCode(68)-Compare Version Numbers的更多相关文章

  1. LeetCode(165) Compare Version Numbers

    题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...

  2. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  3. LeetCode(68) Text Justification

    题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...

  4. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  5. 【leetcode❤python】 165. Compare Version Numbers

    #-*- coding: UTF-8 -*-class Solution(object):    def compareVersion(self, version1, version2):       ...

  6. LeetCode(68):文本左右对齐

    Hard! 题目描述: 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用“贪心算法”来放置给定的单词:也就是 ...

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

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

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

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

  9. 165. Compare Version Numbers - LeetCode

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

随机推荐

  1. 剑指Offer——银行网申内容模版

    剑指Offer--银行网申内容模版 年获得"优秀共青团员"称号 2013.12 科技标兵 2013.10 三好学生 2013.10 "三下乡"优秀学生 2013 ...

  2. Servlet读取文件的最好的方式

    在java web 开发的时候不可避免的会读取文本信息,但是方式不同,所付出的代价也是不一样的,今天学到了一个比较好的实用性的技巧,拿来与大家分享一下. 读取属性配置文件 之所以说成是读取属性(pro ...

  3. ORACLE--Connect By、Level、Start With的使用(Hierarchical query-层次查询)

    查找员工编号为7369的领导: 1 SELECT LEVEL,E.* FROM EMP E CONNECT BY PRIOR E.MGR = E.EMPNO START WITH E.EMPNO = ...

  4. 高性能nosql ledisdb设计与实现 (2):replication

    ledisdb现在已经支持replication机制,为ledisdb的高可用做出了保障. 使用 假设master的ip为10.20.187.100,端口6380,slave的ip为10.20.187 ...

  5. MTK机器原始OTA更新方法

    在源码中编译完成后会生成各类.img的文件,这时候make otapackage生成ota包 一般ota包在源码工程的out/target/...目录下 一.通过线刷模式 将生成OTA包拷贝到Wind ...

  6. Fragment详解-android学习之旅(四十八)

    Fragment的设计哲学 Fragment的继承体系 Fragment的开发 大部分都会继承如下的三个方法 Fragment与Activity的通信 Fragment与Activity交互信息 Fr ...

  7. 【一天一道LeetCode】#119. Pascal's Triangle II

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

  8. GROUP BY 的实现与优化

    由于GROUP BY实际上也同样需要进行排序操作,而且与ORDER BY相比,GROUP BY主要只是多了排序之后的分组操作.当然,如果在分组的时候还使用了其他的一些聚合函数,那么还需要一些聚合函数的 ...

  9. JAVA代理机制

    JAVA代理相关主要知识如下: (1)利用代理可以在运行时创建一个实现了一组给定接口的新类.         这种功能只有在编译时无法确定需要实现哪个接口时才有必要使用. (2)假设有一个表示接口的C ...

  10. EXCEPTION与ERROR的区别

    EXCEPTION与ERROR的区别