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. AD域服务器|两台DC无法进行复制同步

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 说明:前段时间公司两台域控出现了一些问题导致数据无法相互进行同步,DC之间也无法进行共享访问,网络用户无法通过计算机名映射的共享访问 ...

  2. java 练手 Fibonacci数

    Problem B Fibonacci数 时间限制:3000 ms  |  内存限制:65535 KB   描述 无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列 ...

  3. nyoj 236拦截导弹 简单动归(java)

    C/C++: #include<stdio.h> int main() { // freopen("250.txt","r",stdin); ],b ...

  4. ASP.NET、C#调用外部可执行exe文件--多种方案

    一. try { //方法一 //调用自己的exe传递参数 //Process proc = new Process(); //proc.StartInfo.FileName = @"D:\ ...

  5. Leonbao:MapKit学习笔记

    以下仅作了解, 实际使用以百度地图居多, 因为百度地图有动态路径规划等接口 MapKit学习笔记    原帖: http://www.cocoachina.com/bbs/read.php?tid-6 ...

  6. OOP感想

    OOP是面向对象编程(Object Oriented Programming).集于一身,最终目的是各司其职,让每个职责的只关注自己那块,其他的不管丢给下一个人.比如说,一个页面,对于客户,只要能看到 ...

  7. Android实用代码模块集锦

    1. 精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) 1 2 3 4 5 6 public static double getScreenPhysicalSize(Activity ctx)  ...

  8. Bloom Filter 概念和原理

    Bloom filter 是由 Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是不是集合中的一个成员.如果检测结果为是,该元素不一定 ...

  9. ndk学习20: jni之OnLoad动态注册函数

    一.原理 当在系统中调用System.loadLibrary函数时,该函数会找到对应的动态库, 然后首先试图找到"JNI_OnLoad"函数,如果该函数存在,则调用它 JNI_On ...

  10. UITableView 系列之自定义 UITableViewCell

    http://www.360doc.com/content/14/0225/14/11029609_355567657.shtml