LeetCode(68)-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
思路:
- 题意:比较两个版本号字符串的大小
- 把字符串用split转化为数组,注意split(\.),然后转化为整数数组,遍历比较。注意如果版本号后面都是零的情况
代码:
public class Solution {
public int compareVersion(String version1, String version2) {
String[] v1,v2;
if(version1.indexOf(".") == -1){
v1 = new String[1];
v1[0] = version1;
}else{
v1 = new String[version1.split("\\.").length];
v1 = version1.split("\\.");
}
if(version2.indexOf(".") == -1){
v2 = new String[1];
v2[0] = version2;
}else{
v2 = new String[version2.split("\\.").length];
v2 = version2.split("\\.");
}
int[] array1 = sToInt(v1);
int[] array2 = sToInt(v2);
int nn = Math.min(array1.length,array2.length);
for(int a = 0;a < nn;a++){
if(array1[a] > array2[a]){
return 1;
}else if(array1[a] < array2[a]){
return -1;
}
}
if(array1.length > array2.length){
for(int k = nn; k < array1.length;k++){
if(array1[k] != 0){
return 1;
}
}
return 0;
}else if(array1.length < array2.length){
for(int m = nn;m < array2.length;m++){
if(array2[m] != 0){
return -1;
}
}
return 0;
}
return 0;
}
public int[] sToInt(String[] ss){
int n = ss.length;
int[] result = new int[n];
for(int i = 0;i < n;i++){
try{
result[i] = Integer.parseInt(ss[i]);
}catch(Exception e){
}
}
return result;
}
}
LeetCode(68)-Compare Version Numbers的更多相关文章
- LeetCode(165) Compare Version Numbers
题目 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- LeetCode(68) Text Justification
题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【leetcode❤python】 165. Compare Version Numbers
#-*- coding: UTF-8 -*-class Solution(object): def compareVersion(self, version1, version2): ...
- LeetCode(68):文本左右对齐
Hard! 题目描述: 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用“贪心算法”来放置给定的单词:也就是 ...
- 【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 ...
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
随机推荐
- 03SpringMVC,Spring,Hibernate整合(Date时间转换)
项目结构 2 web.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8"?> <web-app ...
- python使用qq服务器发送邮件
python使用qq服务器发送邮件 直接上代码: #!/usr/bin/python2.7 #-*- coding: UTF-8 -*- # sendmail.py # # init created: ...
- HTML5中 基本用法及属性 韩俊强的博客
从今天开始更新H5相关学习:希望大家能一起学习,多学习一门语言,多一门乐趣! 了解Html5: Html5基本属性: <!DOCTYPE html> <html lang=" ...
- Android初级教程使用服务注册广播接收者监听手机解锁屏变化
之前第七章广播与服务理论篇写到: 特殊的广播接收者(一般发广播次数频率很高) 安卓中有一些广播接收者,必须使用代码注册,清单文件注册是无效的 屏幕锁屏和解锁 电量改变 今天在这里就回顾一下,且用代码方 ...
- 最简单的基于FFmpeg的内存读写的例子:内存播放器
===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...
- iOS 图片裁剪与修改
最近做的项目中需要上传头像,发表内容的时候也要涉及到图片上传,我直接用的原图上传,但是由于公司网络差,原图太大,老是加载好久好久,所以需要把原图裁剪或者修改分辨率之后再上传,找了好久,做了很多尝试才解 ...
- 物料事务处理interface与temp解析
MTL_TRANSACTIONS_INTERFACE MTL_TRANSACTIONS_INTERFACE is the interface point between non– Inventory ...
- 深度剖析malloc、free和new、delete
1.malloc,free是C语言的函数,而new,delete是操作符,属于C++的语法,一定注意这两个不再是函数了,而是操作符. 2.malloc和new对于分配基础类型变量和数组变量,它们除了语 ...
- volatile实现可见性但不保证原子性
volatile实现可见性但不保证原子性 volatile关键字: 能够保证volatile变量的可见性 不能保证volatile变量复合操作的原子性 volatile如何实现内存可见性: 深入来说: ...
- General Ledger Useful SQL Scripts
General Ledger Useful SQL Scripts – Oracle Applications 11i Contents GL Set of Books Configuration O ...