LeetCode—Compare Version Numbers,leetcodecompare

來源:互聯網
上載者:User

LeetCode—Compare Version Numbers,leetcodecompare

Compare two version numbers version1 and version1.
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

剛拿到題目的時候主要是對題目理解了很久啊

其實意思很簡單,每一個字串中有一些數字和‘.’,‘.’的作用只是為了把數字分開,然後依次比較大小

在自己寫的很土的方法中,就是簡單的先把兩個string分解到對應的vector當中,這裡有幾個需要注意的地方

兩個字串中的數字長度不是相等的,所以如果前面都相等還要看後面

11.22.33.00.00000 = 11.22.33

11.22.33.01 > 11.22.33

還有就是字串最後面是沒有'.'符號的,這裡需要注意

class Solution {public:    int compareVersion(string version1, string version2) {        vector<int> ver1;vector<int> ver2;int val1 = 0,val2 = 0;int i = 0,j = 0;while (i < version1.length()){if(version1[i] != '.'){val1 = val1*10+(version1[i]-'0');}else{ver1.push_back(val1);val1 = 0;}i++;}while (j < version2.length()){if (version2[j] != '.'){val2 = val2*10+(version2[j]-'0');}else{ver2.push_back(val2);val2 = 0;}j++;}ver1.push_back(val1);ver2.push_back(val2);bool flag = ver1.size() <= ver2.size();int length = flag? ver1.size():ver2.size();for (i = 0; i < length; i++){if (ver1[i] > ver2[i]){return 1;}else if(ver1[i] < ver2[i]){ return -1;}}if (flag){for (i = length; i < ver2.size(); i++){if (ver2[i] != 0){return -1;}}return 0;}else{for (i = length; i < ver1.size(); i++){if (ver1[i] != 0){return 1;}}return 0;}    }};

下面是網上的一些別的演算法,思想上都大同小異,但是代碼因為在while迴圈內,而且只迴圈一遍著實是簡便了很多:
class Solution {public:    int compareVersion(string version1, string version2) {        int lev1=0,lev2=0;        int id1=0,id2=0;        while(id1!=version1.length()||id2!=version2.length()){            lev1=0;            while(id1<version1.length()){                if(version1[id1]=='.'){                    ++id1;                    break;                }                lev1=lev1*10+(version1[id1]-'0');                ++id1;            }                        lev2=0;            while(id2<version2.length()){                if(version2[id2]=='.'){                    ++id2;                    break;                }                lev2=lev2*10+(version2[id2]-'0');                ++id2;            }                        if(lev1>lev2){                return 1;            }else if(lev1<lev2){                return -1;            }        }        return 0;//equal    }};



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.