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

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。 您可以假设版本字符串是非空的并且只包含数字和 . 特点。 这 。 字符不代表小数点,用于分隔数字...

    js-leetcode题解之165-compare-version-numbers.js

    javascript js_leetcode题解之165-compare-version-numbers.js

    python-leetcode题解之165-Compare-Version-Numbers.py

    python python_leetcode题解之165_Compare_Version_Numbers.py

    java-leetcode题解之第165题比较版本号.zip

    public int compareVersion(String version1, String version2) { String[] nums1 = version1.split("\\."); String[] nums2 = version2.split("\\."); for (int i = 0; i (nums1.length, nums2.length); i++) {...

    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.你用过哪些...

    java8-spring5部分技术与工具介绍.pdf

    Optional&lt;Integer&gt; max = numbers.stream().max(Integer::compare); Optional&lt;Integer&gt; min = numbers.stream().min(Integer::compare); Integer sum = numbers.stream().reduce(0, Integer::sum); Double ...

    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 ...

    Parasoft Handbook

    1. **Step 1**: Select the two files or directories you want to compare. 2. **Step 2**: Run the DIFF tool. 3. **Step 3**: Review the differences highlighted. 4. **Step 4**: Resolve any discrepancies as...

    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 ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - **Objective:** Compare two dictionaries and find differences. - **Key Concepts:** - Iterating over dictionary keys. - Comparing values and handling missing keys. 17. **How Many Different ...

    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 ...

    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_...

    BASIC-ABC:自动化测试的简单工具-开源

    4. **Compare Demo**: 这可能是一个演示或示例,用于展示如何对比测试结果,对于验证和调试自动化测试过程非常有用。 5. **Nude Shell**: 与"Nude Shell Version 2.0"相似,可能是用于执行命令的界面,但可能不包含...

    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, ...

Global site tag (gtag.js) - Google Analytics