国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] Compare Version Numbers

Alex / 2164人閱讀

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.

Example

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

Note

https://stackoverflow.com/que...
Basically if you want to split a dot ".", the regex "." means "any character", so you need to escape the dot with two backsplashes.

Solution
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++) {
            Integer i1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
            Integer i2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;
            int compare = i1.compareTo(i2);
            if (compare != 0) return compare;
        }
        return 0;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/69284.html

相關(guān)文章

  • [Leetcode] Compare Version Numbers 比較版本號(hào)

    摘要:注意因?yàn)榉椒ㄝ斎氲氖且粋€(gè)正則表達(dá)式所以不能直接用,而是要用,而的要轉(zhuǎn)義,所有要用代碼按照進(jìn)行分割比對(duì)相應(yīng)的子串如果某個(gè)版本號(hào)更長(zhǎng),判斷其多余部分是否是,如果不是,則較長(zhǎng)的較大,否則是一樣的。 Compare Version Numbers Compare two version numbers version1 and version2. If version1 > version2...

    FrozenMap 評(píng)論0 收藏0
  • [LeetCode] Compare Version Numbers

    摘要:首先找整數(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...

    jzman 評(píng)論0 收藏0
  • [LeetCode] 165. Compare Version Numbers

    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...

    趙春朋 評(píng)論0 收藏0
  • leetcode165. Compare Version Numbers

    摘要:題目要求也就是說(shuō),比較版本號(hào)。思路一利用通過(guò)方法將版本通過(guò)分隔開(kāi),然后將每一段版本從轉(zhuǎn)化為進(jìn)行比較思路二自己實(shí)現(xiàn)轉(zhuǎn)化為自己實(shí)現(xiàn)將轉(zhuǎn)化為,可以通過(guò)循環(huán)的方式。這是一個(gè)基本的算法。 題目要求 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < ve...

    Mike617 評(píng)論0 收藏0
  • [LintCode/LeetCode] Find Median From / Data Stream

    摘要:建立兩個(gè)堆,一個(gè)堆就是本身,也就是一個(gè)最小堆另一個(gè)要寫(xiě)一個(gè),使之成為一個(gè)最大堆。我們把遍歷過(guò)的數(shù)組元素對(duì)半分到兩個(gè)堆里,更大的數(shù)放在最小堆,較小的數(shù)放在最大堆。同時(shí),確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...

    zxhaaa 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<