题目:

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. linux下连接windows的远程桌面

    拿ubuntu来举例: 1安装rdesktop 2 rdesktop -f 196.168.1.11:3389 3 哦鸟

  2. 小文本——Cookies

    http协议的无状态性导致在需要会话的场景下寸步难行,例如一个网站为了方便用户,在一段时间内登录过改网站的浏览器客户端实现自动登录,为实现这种客户端与服务器之间的会话机制需要额外的一些标识,http头 ...

  3. 【一天一道LeetCode】#371. Sum of Two Integers

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

  4. MySQL语句高效写法整理

    优先使用INNER JOIN 多表关联查询,扫描的行尽量少         关联的时候下条件减少扫描的行数 SELECT     ... FROM     ad_ad_summary_for_pos_ ...

  5. Dynamics CRM 2013 subgrid刷新后刷新主表单

    项目中会遇到这样的需求,会根据subgrid中的数据变化去更新主表单中的某个或多个字段值,一般的做法就是写插件根据subgrid中实体数据的变化在后台更新主表数据库,但后台更新了要反映到前台就得使用刷 ...

  6. Strategy 设计模式 策略模式 超靠谱原代码讲解

    先来假设一种情,我们需要向三种不同的客户做出不同的报价,一般来说要肿么设计呢,是不是马上会想到用IF,没有错,对于这种情况,策略模式是最好的选.大家可以这么理解,如果有情况需要用到大量的IF,那你用策 ...

  7. 9.2.2、Libgdx的输入处理之事件处理

    (官网:www.libgdx.cn) 事件处理可以更加准确的获取用户的输入.事件处理提供了一种可以通过用户接口进行交互的方法.比如按下.释放一个按钮. 输入处理 事件处理通过观察者模式来完成.首先,需 ...

  8. (NO.00001)iOS游戏SpeedBoy Lite成形记(二十四)

    我们回到Xcode,打开GameScene.m,首先要添加实例变量: CCNode *_trackLine; 为了根据选中的赛道更新_trackLine的位置,我们添加一个显示方法: -(void)s ...

  9. (NO.00001)iOS游戏SpeedBoy Lite成形记(十三)

    游戏特效部分就先这样了,因为毕竟是Lite版本,而且是第一个App,所以咱们把主要精力放在游戏可玩逻辑上吧(虽然已经厚颜无耻的加了不少特效了). 说句题外话:游戏美工是独立开发者不可逾越的鸿沟,是无法 ...

  10. Android studio使用git-android学习之旅(79)

    首先我参考了hello_my_show和梦痕_sky的博客,表示感谢 android studio对于git的支持是很好的,这节课我们拉讲解怎么使用git可视化工具来clone project和提交修 ...