【leetcode】Compare Version Numbers(middle)
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
思路:
以 . 为分隔符分割数字,依次对比大小。注意两个版本号长度不一样的情况。
int compareVersion(string version1, string version2) {
int i = , j = ;
int n1 = , n2 = ;
while(i < version1.size() && j < version2.size()) //对比每一个小数点前对应的数字
{
n1 = , n2 = ;
while(i < version1.size() && version1[i++] != '.')
n1 = n1 * + version1[i - ] - '';
while(j < version2.size() && version2[j++] != '.')
n2 = n2 * + version2[j - ] - '';
if(n1 > n2) return ;
if(n1 < n2) return -;
}
//处理数字数量不一样多的情况 如 1.0 和 1 或 1.0.0.4 和 1.0 此时肯定比较短的那个版本号已经到头了 只要获取剩下的那个版本号后面的数字是否有大于0的即可
n1 = , n2 = ;
while(i++ < version1.size())
n1 = (version1[i - ] == '.') ? n1 : n1 * + version1[i - ] - '';
while(j++ < version2.size())
n2 = (version2[j - ] == '.') ? n2 : n2 * + version2[j - ] - '';
if(n1 > n2) return ;
else if(n1 < n2) return -;
else return ;
}
大神的代码,简洁很多。相当于把我的代码下面的循环部分和上面的融合在一起了。
public class Solution {
public int compareVersion(String version1, String version2) {
String[] v1 = version1.split("\\.");
String[] v2 = version2.split("\\.");
int longest = v1.length > v2.length? v1.length: v2.length;
for(int i=0; i<longest; i++)
{
int ver1 = i<v1.length? Integer.parseInt(v1[i]): 0;
int ver2 = i<v2.length? Integer.parseInt(v2[i]): 0;
if(ver1> ver2) return 1;
if(ver1 < ver2) return -1;
}
return 0;
}
}
【leetcode】Compare Version Numbers(middle)的更多相关文章
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode】 search Insert Position(middle)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- C#之XMAL与WPF
XAML的简单说明 XAML是用于实例化.NET对象的标记语言,主要用于构建WPF的用户界面 XAML中的每一个元素都映射为.NET类的一个实例,例如<Button>映射为WPF的Butt ...
- LR测试登陆后进行的操作时 绕过登录
oadrunner web_add_cookie web_add_cookie 这个的函数原来真的能过逃过登录,哈哈,这个苦苦纠结我的问题呀. 函数原型:int web_add_cookie( con ...
- 计算字符数组长度,用strlen 与 sizeof 的原理与区别
遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...
- “北航Clubs”功能规格说明书
1.项目目标说明: 北航Clubs的初衷是服务社团,服务学生,满足社团与学生的信息互通的需求.社团管理运营方便的需求. 建设网站的目的是使学生可以在一个权威可信的网站上获取到社团实时的动态,社团活动的 ...
- iOS开发中乱用hook可能导致灾难
今天有同事遇到问题,他重写viewDidAppear:方法,但是,代码并没有执行到.后来我发现,是另个一同事用了黑魔法搞的鬼,而且他本人并不知道这么做会产生影响.(本文中所有黑魔法指Swizzle) ...
- SQL Server 查询、搜索命令、语句
--查询所有表 SELECT NAME,* FROM SYSOBJECTS WHERE XTYPE='U' order by SYSOBJECTS.name --查询所有存储过程 select * f ...
- [Asp.net MVC]Asp.net MVC5系列——添加数据
目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...
- Android应用性能优化
整理自http://androidperformance.com的几篇博客 代码内存优化-Java篇 避免创建不必须的对象,虽然GC可以回收不用的对象,但为对象分配内存和回收它们同样是需要消耗资源的. ...
- r-cnn学习(一)
http://closure11.com/rcnn-fast-rcnn-faster-rcnn%E7%9A%84%E4%B8%80%E4%BA%9B%E4%BA%8B/ 首先看fast r-cnn这篇 ...
- BZOJ 4579: [Usaco2016 Open]Closing the Farm
Description 依次删去一个点和它的边,问当前图是否连通. Sol 并查集. 倒着做就可以了. 每次将一个点及其的边加入,如果当前集合个数大于 1,那么就不连通. Code /******** ...