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-广播

    #!/usr/bin/python #coding=utf-8 #广播端 import sys,socket import time s=socket.socket(socket.AF_INET,so ...

  2. Vue单文件模板实例

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

  3. ubuntu下终端路径显示的修改

    环境:ubuntu16.04 ubuntu在默认情况下是显示绝对路径的,进入目录过长的时候让人感觉很不舒服,现在修改成只显示当前目录 vim ~/.bashrc 找到这句 # If this is a ...

  4. 2-7 js基础-ajax封装

    function json2url(json) { var arr = []; for (var name in json) { arr.push(name+'='+encodeURIComponen ...

  5. InnoDB存储引擎的表空间文件,重做日志文件

    存储引擎文件:因为MySQL表存储引擎的关系,每个存储引擎都会有自己的文件来保存各种数据.这些存储引擎真正存储了数据和索引等数据. 表空间文件 InnoDB存储引擎在存储设计上模仿了Oracle,将存 ...

  6. JVM的类加载时机

    类加载过程中每个步骤的顺序 我们已经知道,类加载的过程包括:加载.连接.初始化,连接又分为:验证.准备.解析,所以说类加载一共分为5步:加载.验证.准备.解析.初始化. 其中加载.验证.准备.初始化的 ...

  7. Go RabbitMQ(三)发布订阅模式

    RabbitMQ 在上一节中我们创建了工作队列,并且假设每一个任务都能够准确的到达对应的worker.在本节中我们将介绍如何将一个消息传递到多个消费者,这也就是所说的发布订阅模式 为了验证该模式我们使 ...

  8. SpringCloud - 2. 服务注册 和 发现

    SpringCloud 的服务注册和发现是由Eureka来完成. 1.eureka server 1.1 依赖 <dependency> <groupId>org.spring ...

  9. 微信授权获取code(微信支付)

    摘要:最近在做h5支付,然后发现一个问题,微信自带浏览器不支持h5支付,然后后台又做了一个微信支付的接口,然后要传code参数,代码写好总结后,就发到这里记录一下: 因为有两个支付接口,所以首先判断打 ...

  10. C# 实现将listview中已经显示的数据导出到Access 数据库

    private void button1_Click(object sender, EventArgs e) { OleDbConnection dbconn = new OleDbConnectio ...