leetcode刷題--Compare Version Numbers
題目:比較兩個版本號,如果第一個版本高就返回整數1,第二個高就返回整數-1,一樣就返回0
簡單的思路是字串處理,根據"."的位置拆分字串到列表中,如果兩個列表不等長就用0填充至等長,然後從列表第一位開始對比列表中相同位置的數字大小
程式碼:
class Solution:
# @param {string} version1
# @param {string} version2
# @return {integer}
def compareVersion(self, version1, version2):
vlist1=version1.split(".")
vlist2=version2.split(".")
add = [0 for i in range(min(len(vlist1),len(vlist2)),max(len(vlist1),len(vlist2)))]
if len(vlist1) > len(vlist2):
vlist2 = vlist2+add
if len(vlist1) < len(vlist2):
vlist1 = vlist1+add
i=0
while i < len(vlist2):
if int(vlist1[i]) > int(vlist2[i]):
return 1
break
elif int(vlist1[i]) < int(vlist2[i]):
return -1
break
else:
i+=1
return 0
相關文章
- Leetcode 165 Compare Version NumbersLeetCode
- 跟我一起刷leetCode演算法題11之 Maximum Product of Three NumbersLeetCode演算法
- LeetCode刷題整理LeetCode
- leetcode刷題(一)LeetCode
- LeetCode刷題 堆LeetCode
- LeetCode 刷題—樹LeetCode
- LeetCode 刷題指南(一):為什麼要刷題LeetCode
- LeetCode 刷題筆記LeetCode筆記
- leetcode刷題筆記LeetCode筆記
- LeetCode刷題記錄LeetCode
- 如何使用leetcode刷題LeetCode
- leetcode刷題--Majority ElementLeetCode
- leetcode刷題--Count PrimesLeetCode
- leetcode刷題--Reverse BitsLeetCode
- leetcode刷題--Rotate ArrayLeetCode
- leetcode刷題--Count and SayLeetCode
- Leetcode刷題分類LeetCode
- Leetcode刷題準備LeetCode
- LeetCode刷題—陣列LeetCode陣列
- php版本比較函式version_compare()PHP函式
- Leetcode - Bitwise AND of Numbers RangeLeetCode
- LeetCode-Lexicographical NumbersLeetCode
- Leetcode刷題——求眾數LeetCode
- leetcode刷題筆記605LeetCode筆記
- Leetcode刷題系列彙總LeetCode
- leetcode刷題--Happy NumberLeetCodeAPP
- leetcode刷題--Number of 1 BitsLeetCode
- leetcode刷題記錄 661~LeetCode
- LeetCode刷題之第701題LeetCode
- LeetCode-Bitwise AND of Numbers RangeLeetCode
- Leetcode Sum Root to Leaf NumbersLeetCode
- Leetcode Add Two NumbersLeetCode
- LeetCode 解題報告 - 2. Add Two NumbersLeetCode
- Leetcode已刷題目題解彙總LeetCode
- leetcode排序專題演算法刷題LeetCode排序演算法
- 用Rust刷leetcode第十題RustLeetCode
- 用Rust刷leetcode第八題RustLeetCode
- 用Rust刷leetcode第九題RustLeetCode