【Leetcode】【Easy】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
解题:
题目的意思是,比较版本号的大小,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的更多相关文章
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【刷题-LeetCode】165 Compare Version Numbers
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- 2016.5.21——Compare Version Numbers
Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A' ) 2.srt.at(),substr ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- ehcache 集群使用 rmi方式 有图有真想
来源:http://www.tuicool.com/articles/MJzYZbR ehcache 有几种方式集群 ,rmi,jgroup还有jms:这里讲一下ehcache的使用 ehcache ...
- python-Unix套接字
#!/usr/bin/python #coding=utf-8 #server import socket import sys import os server_address = './test' ...
- linux mint19 解决docker必须使用sudo问题
1 安装完docker 使用时,提示权限不够 ~$ docker info Got permission denied while trying to connect to the Docker da ...
- docker私有仓库搭建及认证
什么是docker? Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机 ...
- Nginx 的信号控制
摘自:Nginx服务器初识:Nginx启动.停止与信号控制 名称 功能 说明 HUP 重启 QUIT 从容关闭 TERM 快速关闭 INT 从容关闭 USR1 切换日志文件 通常用在切 ...
- 使用Symantec代码签名证书对代码进行签名的 5 个理由
借助 Symantec Code Signing,在更多平台上将您的代码提供给更多客户,我们总结了5大理由告诉软件开发者在发布自己的软件时一定要购买Symantec 代码签名证书签名即将发布的软件. ...
- WEB Front-end Development Technology
1.Einführung der HTML(Hypertext Markup Language) 1.1 Grundlegendes Label 1.1.1 Block Label <h1> ...
- Shell脚本编写1
1.shell操作系统与外部最主要的接口就叫做shell.shell是操作系统最外面的一层.shell管理你与操作系统之间的交互:等待你输入,向操作系统解释你的输入,并且处理各种各样的操作系统的输出结 ...
- 方法执行一次js
var isFirst = true; $(function () { //一级 $("#City").change(function () { var url = "/ ...
- Vue生命周期学习
转自https://www.w3cplus.com/vue/vue-instances-and-life-cycles.html Vue实例虽然没有完全遵循MVVM模型,但Vue的设计无疑受到了它的启 ...