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. python-TCP模拟ftp文件传输

    #!/usr/bin/python #coding=utf-8 #server from socket import* import sys,os def command(): l=[ "W ...

  2. Vue单文件模板实例

    AddItemComponent.vue <template> <div id="add-item-template"> <div class=&qu ...

  3. [中英对照]vmlinuz Definition | vmlinuz的定义

    vmlinuz Definition | vmlinuz的定义 vmlinuz is the name of the Linux kernel executable.vmlinuz是Linux内核可执 ...

  4. module使用和设置

    Modules environmentDescription This is a system that allows you to easily change between different v ...

  5. weblogic升级之ddconverter

    1. weblogic8.x 升到weblogic10时,需要升级ejb响应的描述符,否则会报错. BEA-011114 - Error: For EJB modules, deployment pl ...

  6. 【LeetCode题解】144_二叉树的前序遍历

    目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...

  7. ora-01747:因为表中存在关键字造成的

    ORCLE报错解决(ora-01747:无效的用户.表.列,表.列)  一.ora-01747:无效的用户.表.列,表.列 这个问题出现是因为表中存在关键字造成的,如果想新增数据直接用sql语句,查询 ...

  8. NodeJS入门篇

    在我印象里,“全栈工程师”这个词是NodeJS诞生后才逐渐火起来的,因为NodeJS赋予了JS服务器开发的能力.下面开始从一个小白的角度进军NodeJS... 前言:在学习NodeJS之前是需要安装的 ...

  9. java调用ruby代码

    问题: 最近在做一个应用的时候碰到了一个问题.客户端需要调用服务器端传回的脚本信息,然后执行.其中脚本类型包括ruby.而java中调用ruby的代码大致如下: String jrubyCode=&q ...

  10. JAVA-2NIO之Channel

    注意:转载自并发编程网 – ifeve.com本文链接地址: Java NIO系列教程(二) Channel Channel Java NIO的通道类似流,但又有些不同: 既可以从通道中读取数据,又可 ...