【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 ...
随机推荐
- jquery 获取datagrid行数
var data = $('#dg').datagrid('getData'); alert('总数据量:' + data.total)//注意你的数据源一定要定义了total,要不会为undefin ...
- 新年新技术:MongoDB 3.0
前一篇介绍了HTTP/2,这一篇简单介绍下3月3号发布的MongoDB 3.0. What’s new in MongoDB 3.0? 新的存储引擎WiredTiger MongoDB 3.0的存储引 ...
- javacomm64位用不了,可以使用RXTXcomm for x64
安装完后把导入包名改一下就行了! 附上读串口代码: /* * @(#)SimpleRead.java 1.12 98/06/25 SMI * * Copyright (c) 1998 Sun Micr ...
- 北美IT公司大致分档
北美IT公司大致分档(from mitbbs.com) 第一档: Uber, Snapchat, Airbnb, Dropbox, Pinterest 第二档:Facebook, LinkedIn, ...
- cpu利用率和cpu 队列
SIP的第四期结束了,因为控制策略的丰富,早先的的压力测试结果已经无法反映在高并发和高压力下SIP的运行状况,因此需要重新作压力测试.跟在测试人员后面做了快一周的压力测试,压力测试的报告也正式出炉,本 ...
- androids-addjavascriptinterface-equivalent-in-ios
http://stackoverflow.com/questions/7103159/androids-addjavascriptinterface-equivalent-in-ios
- K-V-O 键值观察机制
在两个不同的控制器之间传值是iOS开发中常有的情况,应对这种情况呢,有多种的应对办法.kvc就是其中的一种,所以,我们就在此解释之. key value observing 键值观察,给人一种高 ...
- 破解Excel保护
一.录制宏 二.停止录制 三.查看录制 四.点击编辑进入VB编辑环境 五.清空原有的内容,copy以下代码 Public Sub 工作表保护密码破解() Const DBLSPACE As Strin ...
- django的前后的结合,search搜索功能案例
利用django的Q()功能可以很好的展开搜索功能 假设我要做个这样的搜索功能
- Python 2.x闭包(enclosure)中的变量访问&修改
http://stackoverflow.com/questions/3190706/nonlocal-keyword-in-python-2-x ---answer---- Python can r ...