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

解题:

题目的意思是,比较版本号的大小,2版本比1版本大,而2.10版本比2.5版本大;

分别取出小数点前和小数点后的数字做比较;

代码:

 class Solution {
public:
int compareVersion(string version1, string version2) {
int len1 = version1.size();
int len2 = version2.size();
int num1 = ;
int num2 = ;
int i = ;
int j = ;
while(i < len1 || j < len2) {
while (i < len1 && version1[i] != '.') {
num1 = num1 * + (version1[i] - '');
i++;
} while (j < len2 && version2[j] != '.') {
num2 = num2 * + (version2[j] - '');
j++;
} if(num1 > num2)
return ;
else if (num1 < num2)
return -; num1 = ;
num2 = ;
i++;
j++;
} return ;
}
};
												

【Leetcode】【Easy】Compare Version Numbers的更多相关文章

  1. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  2. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  4. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  6. 【LeetCode题意分析&解答】35. Search Insert Position

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

  7. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

  8. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

  9. 【一天一道LeetCode】#165. Compare Version Numbers

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

随机推荐

  1. maevn HelloWorld 基本命令

    总结: Mvn clean compile:编译主代码 Mvn clean test:执行测试代码 Mvn clean package:打包 Mvn clean install: 安装到本地仓库 执行 ...

  2. VMware里Ubuntu-14.04-desktop的VMware Tools安装图文详解

    不多说,直接上干货!    前期步骤,请见如下 VMware里Ubuntukylin-14.04-desktop的VMware Tools安装图文详解 我这里,直接,是来说明,Ubuntu-14.04 ...

  3. Javac的命令(-Xlint)

    在OptionName类中的枚举定义如下: XLINT("-Xlint"), XLINT_CUSTOM("-Xlint:"), -Xlint     Enabl ...

  4. String类的substring方法

    下列程序的输出是什么? class A { public static void main(String[] a) {     String v = “base”;      v.concat(“ba ...

  5. [转载]VS2010怎样打开VS2013或者VS2015建立的工程

    VS2010怎样打开VS2013或者VS2015建立的工程 作用:解决vs低版本无法直接打开高版本的工程文件问题. 一.转载出处 http://blog.csdn.net/qq2399431200/a ...

  6. Guava之RateLimiter的设计

    Guava源码中很详尽的解释了RateLimiter的概念. 从概念上看,限流器以配置速率释放允许的请求(permit).如有必要,调用acquire()将会阻塞知道一个允许可用.一旦被获取(acqu ...

  7. Linux笔记-Linux命令初解2

    在看linux过程中,文件属性管理是一个难点,因而作为初学者的我来说,我直接将其放在后面来慢慢研究,因而我个人觉得先学习后面一些知识点之后,回过头来将一些你所不懂的去解透,这是极好的意见事情.对了,我 ...

  8. 初识DataGridView 表格数据控件

    DataGridView控件提供了一种强大而灵活的以表格形式显示数据的方式,用户可以使用DataGridView控件来显示少量数据的只读视图,也可以对其进行缩放以显示特大数据集的可编辑视图. 扩展Da ...

  9. 用Lua控制Nginx静态文件的url访问权限

    需求背景:比如我们有一个存储文件的web服务器,一般通过url可直接访问到:http://127.0.0.1/uploads/test.rar,如果我们需要限制别人的访问,可以通过添加lua脚本来控制 ...

  10. grunt-contrib-watch 监控 JS 文件改变来运行预定义的Tasks

    依赖于 GruntJs ~0.4.0 监控 JS 文件改变来运行预定义的Tasks Demo: watch: { scripts: { files: ['src/**/*.js'], tasks: [ ...