Java实现 LeetCode 165 比较版本号
165. 比较版本号
比较两个版本号 version1 和 version2。
如果 version1 > version2 返回 1,如果 version1 < version2 返回 -1, 除此之外返回 0。
你可以假设版本字符串非空,并且只包含数字和 . 字符。
. 字符不代表小数点,而是用于分隔数字序列。
例如,2.5 不是“两个半”,也不是“差一半到三”,而是第二版中的第五个小版本。
你可以假设版本号的每一级的默认修订版号为 0。例如,版本号 3.4 的第一级(大版本)和第二级(小版本)修订号分别为 3 和 4。其第三级和第四级修订号均为 0。
示例 1:
输入: version1 = “0.1”, version2 = “1.1”
输出: -1
示例 2:
输入: version1 = “1.0.1”, version2 = “1”
输出: 1
示例 3:
输入: version1 = “7.5.2.4”, version2 = “7.5.3”
输出: -1
示例 4:
输入:version1 = “1.01”, version2 = “1.001”
输出:0
解释:忽略前导零,“01” 和 “001” 表示相同的数字 “1”。
示例 5:
输入:version1 = “1.0”, version2 = “1.0.0”
输出:0
解释:version1 没有第三级修订号,这意味着它的第三级修订号默认为 “0”。
提示:
版本字符串由以点 (.) 分隔的数字字符串组成。这个数字字符串可能有前导零。
版本字符串不以点开始或结束,并且其中不会有两个连续的点。
class Solution {
public int compareVersion(String version1, String version2) {
String[] a1 = version1.split("\\.");
String[] a2 = version2.split("\\.");
for(int n = 0; n < Math.max(a1.length, a2.length); n++){
int i = (n < a1.length ? Integer.valueOf(a1[n]) : 0);
int j = (n < a2.length ? Integer.valueOf(a2[n]) : 0);
if(i < j) return -1;
else if(i > j) return 1;
}
return 0;
}
}
Java实现 LeetCode 165 比较版本号的更多相关文章
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- [hdu5199]统计数据的水题
题意:统计一个数出现了多少次,统计后删去它所有的出现.思路:乱搞..自己没事写的hash,不过赶脚效率有点低. #pragma comment(linker, "/STACK:1024000 ...
- 黑马程序员_毕向东_Java基础视频教程——位运算符(随笔)
位运算符 左移和右移 左移 左移越移越大. 往左移几位就相当于这个数乘于2的几次方 3 << 2 --> 3 * 2^2 = 3 * 4 = 12 3 << 3 --&g ...
- P5410 【模板】扩展 KMP
P5410 [模板]扩展 KMP #include<bits/stdc++.h> using namespace std; ; int q, nxt[maxn], extend[maxn] ...
- Angular知识点复习
Angular第三方UI组件库(github搜“awesome angular ")-----lonic 概述:是一个第三方的适用于移动端App的UI组件库,可以与Angular/React ...
- day07:集合的使用0220
list_1=set([4,5,6,7])list_2=set([4,8,9])list_3=set([4,5])list_4=set([6,7])a = (2,3)b = (2) #list_3是l ...
- 微软 Build 大会发布大量开发工具与服务!编码、协作、发布,如丝般顺滑
Microsoft Build 2020开发者大会已经圆满落幕,在连续两天48小时的不间断直播中,来自全世界的开发者共赴盛宴,场面相当壮观.在这一年一度的大聚会里,微软也是诚意满满,带来了一连串的产品 ...
- 王艳 201771010127《面向对象程序设计(java)》第七周学习总结
1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: (2)掌握Object类的常用API用法: (3)掌握ArrayList类用法与常用API: (4)掌握枚举类使用方法: (5)结合 ...
- CF551B
题目链接:http://codeforces.com/contest/551/problem/B 题目大意:给出字符串a, b, c.试图合理的安排a的字母顺序,使其中有尽可能多的与 c 或 b 相同 ...
- PHP 调用qq邮箱接口
html代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- SpringCloud Alibaba 简介
SpringCloud Aliababa简介 SpringCloud Alibaba是阿里巴巴集团开源的一套微服务架构解决方案. 微服务架构是为了更好的分布式系统开发,将一个应用拆分成多个子应用,每一 ...