leetcode-5.9[680. 驗證迴文字符串 II、941. 有效的山脈數組、1037. 有效的迴旋鏢](python實現)

題目1

在這裏插入圖片描述

題解1

class Solution:
    def validPalindrome(self, s: str) -> bool:
        l, r = 0, len(s) - 1
        while l < r:
            if s[l] != s[r]:
                case1, case2 = s[l:r], s[l+1:r+1]
                # 如果原數組去頭,去尾的兩個數組有一個爲迴文,則整體爲迴文
                return case1 == case1[::-1] or case2 == case2[::-1]
            l, r = l+1, r-1
        return True

附上題目鏈接

題目2

在這裏插入圖片描述

題解2

class Solution:
    def validMountainArray(self, A: List[int]) -> bool:
        if len(A) < 3:
            return False
        a = A.index(max(A))
        if a == 0 or a == len(A) - 1:
            return False
        for i in range(a):
            if A[i] >= A[i+1]:
                return False
        for i in range(a,len(A)-1):
            if A[i] <= A[i+1]:
                return False
        return True

附上題目鏈接

題目3

在這裏插入圖片描述

題解3

class Solution:
    def isBoomerang(self, points: List[List[int]]) -> bool:
        # 判斷三個點相同
        if not(points[0] != points[1] != points[2]):
            return False
        # 比較斜率
        k1 = (points[1][1]-points[0][1])/(points[1][0]-points[0][0]) if points[1][0]-points[0][0] != 0 else 'non-exist'
        k2 = (points[2][1]-points[1][1])/(points[2][0]-points[1][0]) if points[2][0]-points[1][0] != 0 else 'non-exist'
        return k1 != k2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章