`
hcx2013
  • 浏览: 83042 次
社区版块
存档分类
最新评论

Compare Version Numbers

 
阅读更多

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) {
        int len1 = version1.length();
        int len2 = version2.length();
        int i = 0;
        int j = 0;
        int a = 0;
        int b = 0;
        while (i < len1 || j < len2) {
        	while (i < len1 && version1.charAt(i) != '.') {
        		a = a * 10 + (version1.charAt(i) - '0');
        		i++;
        	}
        	while (j < len2 && version2.charAt(j) != '.') {
        		b = b * 10 + (version2.charAt(j) - '0');
        		j++;
        	}
        	if (a > b) {
        		return 1;
        	} else if (a < b) {
        		return -1;
        	}
        	i++;
        	j++;
        	a = b = 0;
        }
        return 0;
    }
}
 
分享到:
评论

相关推荐

    leetcode卡-CompareVersionNumbers:https://leetcode.com/explore/featured/c

    version1 和 version2。 如果 version1 &gt; version2 返回 1; 如果 version1 &lt; version2 返回 -1;否则返回 0。 您可以假设版本字符串是非空的并且只包含数字和 . 特点。 这 。 字符不代表小数点,用于分隔数字...

    LeetCode最全代码

    201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...

    leetcode算法题主函数如何写-java-interview:java面试总结

    Compare Version Numbers 排列组合 链表 topk大数据 [Github]: Java面试指南 BAT String Reverse 进程 线程 程序 协程 Java 总结 云粒智慧 1.你的项目中你感觉有哪些缺点 2.你的项目的架构图是什么 3.你用过哪些...

    Dundas.Chart.for.Winform.Enterprise.v7.1.0.1812.for.VS2008

    Horizontal Error Bars - Version 6.0 gives developers the ability to add error bars to bar charts, which offers additional statistical information regarding the data on the chart for more in depth ...

    gun tar for windows

    --numeric-owner always use numbers for user/group names -p, --same-permissions extract all protection information --preserve-permissions same as -p -s, --same-order sort names to extract to match ...

    C 语言编缉神经网络工具

    you run the demo.bat program, and you can compare corresponding files to see that they are the same. All you have to do is run "demo.bat" in order to both train and test the batchnet artificial ...

    BobBuilder_app

    In this version I have done away with the b+tree and hash index in favor of my own MGIndex structure which for all intents and purposes is superior and the performance numbers speak for themselves....

    Itanium Architecture For Programmers

    Determining Extensions and Implementation Version Summary References Exercises Appendix A. Command-Line Environments Logging in Logging out Security Text editing Uploading files ...

    AT&T Assembly Language

    The compare instruction 138 Examples of using the flag bits 140 Loops 144 The loop instructions 144 A loop example 145 Preventing LOOP catastrophes 145 Duplicating High-Level Conditional Branches 146 ...

    skein加密解密C源码

    This program is used internally to test/validate/compare different implementations (e.g., Reference, Optimized, Assembly). skein_block_x64.asm This is the 64-bit assembly language version of skein_...

    ImageMagick图片批量处理

    -seed value seed a new sequence of pseudo-random numbers -size geometry width and height of image -stretch type render text with this font stretch -stroke color graphic primitive stroke color -...

    SVN操作手册中文版网页格式

    2. Basic Version-Control Concepts 2.1. 版本库 2.2. 版本模型 2.2.1. 文件共享的问题 2.2.2. 锁定-修改-解锁 方案 2.2.3. 复制-修改-合并 方案 2.2.4. Subversion 怎么做? 2.3. Subversion 实战 2.3.1. ...

    kgb档案压缩console版+源码

    Higher numbers compress better but run slower and use more memory. -3 is the default, and gives a reasonable tradeoff. Recommended options are: -0 to -2 for fast (2X over -3) but poor compression, ...

    Microsoft Codeview and Utilities User's Guide

    Version 2.3 for MS(R) OS/2 and MS-DOS(R) Operating Systems MICROSOFT CORPORATION Information in this document is subject to change without notice and does not represent a commitment on the part of ...

    i-vector的工具箱

    This will make it easier for the users to compare the theory with the implementation and help them better understand the concept behind each algorithm. Usage In ...

    ZendFramework中文文档

    1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) ...2.1.5. 定义访问控制 ...

Global site tag (gtag.js) - Google Analytics