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

思路:

以 . 为分隔符分割数字,依次对比大小。注意两个版本号长度不一样的情况。

int compareVersion(string version1, string version2) {
int i = , j = ;
int n1 = , n2 = ;
while(i < version1.size() && j < version2.size()) //对比每一个小数点前对应的数字
{
n1 = , n2 = ;
while(i < version1.size() && version1[i++] != '.')
n1 = n1 * + version1[i - ] - '';
while(j < version2.size() && version2[j++] != '.')
n2 = n2 * + version2[j - ] - ''; if(n1 > n2) return ;
if(n1 < n2) return -;
}
//处理数字数量不一样多的情况 如 1.0 和 1 或 1.0.0.4 和 1.0 此时肯定比较短的那个版本号已经到头了 只要获取剩下的那个版本号后面的数字是否有大于0的即可
n1 = , n2 = ;
while(i++ < version1.size())
n1 = (version1[i - ] == '.') ? n1 : n1 * + version1[i - ] - '';
while(j++ < version2.size())
n2 = (version2[j - ] == '.') ? n2 : n2 * + version2[j - ] - '';
if(n1 > n2) return ;
else if(n1 < n2) return -;
else return ; }

大神的代码,简洁很多。相当于把我的代码下面的循环部分和上面的融合在一起了。

public class Solution {
public int compareVersion(String version1, String version2) {
String[] v1 = version1.split("\\.");
String[] v2 = version2.split("\\."); int longest = v1.length > v2.length? v1.length: v2.length; for(int i=0; i<longest; i++)
{
int ver1 = i<v1.length? Integer.parseInt(v1[i]): 0;
int ver2 = i<v2.length? Integer.parseInt(v2[i]): 0; if(ver1> ver2) return 1;
if(ver1 < ver2) return -1;
}
return 0;
}
}

【leetcode】Compare Version Numbers(middle)的更多相关文章

  1. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. 【leetcode】Compare Version Numbers

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

  3. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  5. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  8. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. 黑客攻防技术宝典Web实战篇(三)web攻击方式总结

    web攻击的手段无非就是使服务器资源耗尽,使服务器无法接收正常请求. 一.DDos攻击 二.DRDos攻击 三.慢攻击 与Ddos攻击相反,慢攻击并不是以多取胜,而是靠保持连接.

  2. lwfs指定特定目录输出

    在特定节点启lwfs服务,输出特定的目录 在[root@devcpucs ~]# 节点启lwfs服务,输出指定目录/home/export/online1/systest/swcpucs 1.将gio ...

  3. POJ 1925 Spiderman

    Spiderman Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5858 Accepted: 1143 Description ...

  4. CentOS-6.5-NFS部署

                      nfs-server与nfs-client端配置一样   NFS(network file system)网络文件系统:用于在网络上共享存储. 服务端-192.16 ...

  5. IE6对png图片的处理

    在学习phpcms系统搜索模块的时候,发现下面这段代码: <!--[if IE 6]> <script type="text/javascript" src=&q ...

  6. Oracle sysdate 时间加减

    加法 select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate,add_months(sysdate,1 ...

  7. 教你搭建SpringMVC框架( 更新中、附源码)

    一.项目目录结构 二.SpringMVC需要使用的jar包 commons-logging-1.2.jar junit-4.10.jar log4j-api-2.0.2.jar log4j-core- ...

  8. windows下php,redis配置

    在windows环境下搭建redis是一般是为了测试,官方redis并没有给windows版redis但是微软有. windows版下载地址:https://github.com/MSOpenTech ...

  9. 用消息机制解耦Activity跳转

    我见过的Activity方式有三种: 1, 默认的,在一个Activity里创建一个Intent,然后startActivity/startActivityForResult: 2, 给被跳转到的Ac ...

  10. python成长之路 :线程、进程和协程

    python线程 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源的管理和分 ...