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

資訊專欄INFORMATION COLUMN

[LeetCode] Compare Version Numbers

Alex / 2160人閱讀

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;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/69284.html

相關文章

  • [Leetcode] Compare Version Numbers 比較版本號

    摘要:注意因為方法輸入的是一個正則表達式所以不能直接用,而是要用,而的要轉義,所有要用代碼按照進行分割比對相應的子串如果某個版本號更長,判斷其多余部分是否是,如果不是,則較長的較大,否則是一樣的。 Compare Version Numbers Compare two version numbers version1 and version2. If version1 > version2...

    FrozenMap 評論0 收藏0
  • [LeetCode] Compare Version Numbers

    摘要:首先找整數部分的坐標段,和都指向初值,令和一直向后遍歷到小數點為止。然后用將的整數段轉化為數值,進行比較若結果為大于或小于關系,直接返回結果若結果為相等,進行小數部分的比較。 Problem Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < v...

    jzman 評論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...

    趙春朋 評論0 收藏0
  • leetcode165. Compare Version Numbers

    摘要:題目要求也就是說,比較版本號。思路一利用通過方法將版本通過分隔開,然后將每一段版本從轉化為進行比較思路二自己實現轉化為自己實現將轉化為,可以通過循環的方式。這是一個基本的算法。 題目要求 Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < ve...

    Mike617 評論0 收藏0
  • [LintCode/LeetCode] Find Median From / Data Stream

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

    zxhaaa 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<