leetcode刷题记录 661~

661 easy

题目

https://leetcode.cn/problems/image-smoother/description/

思路

遍历移动
时间复杂度O(mn) 空间复杂度O(mn)

代码

点击查看代码
class Solution:
    def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
        col = len(img)
        row = len(img[0])
        ava = [[0 for i in range(row)] for j in range(col)]
        index = [-1,0,1]
        for i in range(col):
            for j in range(row):
                count = 0
                tmp   = 0
                for x in index:
                    if i + x >= 0 and i+x < col:
                        for y in index :
                            if j + y >=0 and j+y < row:
                                count += 1
                                tmp += img[i + x][j + y]
                ava[i][j] = int(tmp/count)
        return ava

662 mid

题目

https://leetcode.cn/problems/maximum-width-of-binary-tree/

思路

广度优先遍历
但是由于需要考虑null节点,因此需要考虑每个节点的位置
二叉树有个默认点,就是左叶子节点=当前节点X2,右叶子节点=当前节点X2+1,因此需要利用这个特性计算每一层的最大值

代码

点击查看代码
class Solution:

    def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        res = 1
        arr = [[root, 1]]
        while arr:
            temp = []
            for node,index in arr:
                if node.left is not None:
                    temp.append([node.left,index*2])
                if node.right is not None:
                    temp.append([node.right,index*2+1])
            res = max(res, arr[-1][1] - arr[0][1] + 1)
            arr = temp
        return res

665 mid

题目

https://leetcode.cn/problems/non-decreasing-array/description/

思路

顺序遍历
如果n[i-1]>n[i+1] 则变大n[i+1] 如果n[i-1]<=n[i+1] 则缩小n[i]

代码

点击查看代码
class Solution:
    def checkPossibility(self, nums: List[int]) -> bool:
        result = True
        hasChange = False
        n = len(nums)
        for i in range(n-1):
            if nums[i]<=nums[i+1]:
                continue
            if i == 0:
                nums[0] = nums[1]
                hasChange = True
                continue
            if not hasChange and nums[i-1] <= nums[i+1]:
                nums[i] = nums[i+1]
                hasChange = True
                continue
            if not hasChange and nums[i-1] > nums[i+1]:
                nums[i+1] = nums[i]
                hasChange = True
                continue
            if hasChange:
                result = False
                break
        return result

669 mid

题目

https://leetcode.cn/problems/trim-a-binary-search-tree/description/

思路

利用二叉搜索树特性,左叶子节点<父节点<右叶子节点

代码

点击查看代码
class Solution:
    def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
        if root is None:
            return None
        elif root.val > high:
            return self.trimBST(root.left,low,high)
        elif root.val < low:
            return self.trimBST(root.right,low,high)
        root.left  = self.trimBST(root.left,low,high)
        root.right = self.trimBST(root.right,low,high)
        return root

670 mid

题目

https://leetcode.cn/problems/maximum-swap/description/

思路

先将num转化为字符串nums 判断 如果字符串是降序排列则不用交换,在判断中遇到不满足条件时跳出 此时i前面满足,则不用管i前面的数,在i后面找到最大的字符,与前面的最大值进行交换

代码

点击查看代码
class Solution:
    def maximumSwap(self, num: int) -> int:
        nums = []
        if num <=10 :
            return num
        while num >= 10:
            cur = num%10
            nums.append(cur)
            num = (num - cur)/10
        nums.append(num)
        for i in range(len(nums)-1,0,-1):
            if nums[i] < nums[i-1]:
                max = i-1
                for j in range(i-1,-1,-1):
                    if nums[j] >= nums[max]:
                        max = j
                for j in range(len(nums)-1,i-1,-1):
                    if nums[max] > nums[j]:
                        tmp = nums[max]
                        nums[max] = nums[j]
                        nums[j] = tmp
                        break
                break
        result = 0
        for i in range(len(nums)-1,-1,-1):
            result = result * 10
            result += nums[i]
        return int(result)

posted @ 2024-10-14 22:53  maoguai  阅读(6)  评论(0编辑  收藏  举报