1. 程式人生 > >leetcode【165】Compare Version Numbers

leetcode【165】Compare Version Numbers

寫在最前面:我承認我菜,對於這道題,我真的無話可說

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.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

比較版本號大小

思路呢,其實也不難,我之前走了很多彎路,還是不理解題意吧,核心思想只有一個,只要不相等,就判斷並返回1或者-1

其餘一律0

然後就是補0,我一開始想著字串轉int在通過長度次方補成相同長度的字串,這樣就帶來一個問題,你補完後根本不知道小數點在哪裡,不建議用re.sub去去掉小數點,反而,小數點是你區分字元的重要依據。

用split分離字元,問題又來了,你的for迴圈迴圈到長度小的字串怎麼辦,重點不在簡化判斷條件程式碼,而是你需要怎麼進入判斷

我的思路是如果迴圈超過短的字串,用0去和長的字串比較。下面上程式碼

class Solution:
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """
        

        newversion1 = version1.split('.')
        newversion2 = version2.split('.')
        len1 = len(newversion1)
        len2 = len(newversion2)
 
        if len1 > len2:
            for i in range(len1):
                if i<len2:
                    if int(newversion1[i]) > int(newversion2[i]):
                        return 1
                    elif int(newversion1[i]) < int(newversion2[i]):
                        return -1
                elif int(newversion1[i]) > 0:
                    return 1
            return 0

        elif len1 < len2:
            for i in range(len2):
                if i<len1:
                    if int(newversion1[i]) > int(newversion2[i]):
                        return 1
                    elif int(newversion1[i]) < int(newversion2[i]):
                        return -1
                elif int(newversion2[i]) > 0:
                    return -1
            return 0
        elif len1 == len2:
            for i in range(len1):
                if int(newversion1[i]) > int(newversion2[i]):
                    return 1
                elif int(newversion1[i]) < int(newversion2[i]):
                    return -1
            return 0

程式碼寫的稀爛啊,但是能過,核心思想把握住即可,不要慌,問題不大。

依舊放一下java程式碼吧

class Solution {
    public int compareVersion(String version1, String version2) {
	    String[] version1Array = version1.split("\\.");
	    String[] version2Array = version2.split("\\.");
	    int index = 0;
	    //獲取最小長度值
	    int minLen = Math.min(version1Array.length, version2Array.length);
	    int diff = 0;
	    //迴圈判斷每位的大小
	    while (index < minLen && (diff = Integer.parseInt(version1Array[index]) - Integer.parseInt(version2Array[index])) == 0) {
	        index++;
	    }
	    if (diff == 0) {
	        //如果位數不一致,比較多餘位數
	        for (int i = index; i < version1Array.length; i++) {
	            if (Integer.parseInt(version1Array[i]) > 0) {
	                return 1;
	            }
	        }

	        for (int i = index; i < version2Array.length; i++) {
	            if (Integer.parseInt(version2Array[i].trim()) > 0) {
	                return -1;
	            }
	        }
	        return 0;
	    } else {
	        return diff > 0 ? 1 : -1;
	    }
    }
}

java就不細講了,晚安