Problem
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.
You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0.
Example 1:
Input: version1 = "0.1", version2 = "1.1"
Output: -1
Example 2:
Input: version1 = "1.0.1", version2 = "1"
Output: 1
Example 3:
Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1
Example 4:
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1”
Example 5:
Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"
Note:
Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
Version strings do not start or end with dots, and they will not be two consecutive dots.
class Solution { public int compareVersion(String version1, String version2) { String[] v1 = version1.split("."); String[] v2 = version2.split("."); int len = Math.max(v1.length, v2.length); for (int i = 0; i < len; i++) { int n1 = i < v1.length ? Integer.parseInt(v1[i]) : 0; int n2 = i < v2.length ? Integer.parseInt(v2[i]) : 0; int ans = Integer.compare(n1, n2); if (ans != 0) return ans; } return 0; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/72994.html
摘要:題目要求也就是說,比較版本號。思路一利用通過方法將版本通過分隔開,然后將每一段版本從轉(zhuǎn)化為進(jìn)行比較思路二自己實(shí)現(xiàn)轉(zhuǎn)化為自己實(shí)現(xiàn)將轉(zhuǎn)化為,可以通過循環(huán)的方式。這是一個基本的算法。 題目要求 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < ve...
摘要:注意因?yàn)榉椒ㄝ斎氲氖且粋€正則表達(dá)式所以不能直接用,而是要用,而的要轉(zhuǎn)義,所有要用代碼按照進(jìn)行分割比對相應(yīng)的子串如果某個版本號更長,判斷其多余部分是否是,如果不是,則較長的較大,否則是一樣的。 Compare Version Numbers Compare two version numbers version1 and version2. If version1 > version2...
Problem 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 an...
摘要:首先找整數(shù)部分的坐標(biāo)段,和都指向初值,令和一直向后遍歷到小數(shù)點(diǎn)為止。然后用將的整數(shù)段轉(zhuǎn)化為數(shù)值,進(jìn)行比較若結(jié)果為大于或小于關(guān)系,直接返回結(jié)果若結(jié)果為相等,進(jìn)行小數(shù)部分的比較。 Problem Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < v...
摘要:建立兩個堆,一個堆就是本身,也就是一個最小堆另一個要寫一個,使之成為一個最大堆。我們把遍歷過的數(shù)組元素對半分到兩個堆里,更大的數(shù)放在最小堆,較小的數(shù)放在最大堆。同時,確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...
閱讀 2454·2021-11-23 09:51
閱讀 503·2019-08-30 13:59
閱讀 1820·2019-08-29 11:20
閱讀 2529·2019-08-26 13:41
閱讀 3239·2019-08-26 12:16
閱讀 729·2019-08-26 10:59
閱讀 3321·2019-08-26 10:14
閱讀 602·2019-08-23 17:21