代码随想录算法公开课day1

704. 二分查找 - 力扣(LeetCode)

# 左闭右开
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums)
        while right > left:
            middle = (right + left) // 2
            if nums[middle] > target:
                right = middle
            elif nums[middle] < target:
                left = middle + 1
            else:
                return middle
        return -1
# 左闭右闭
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums) - 1
        while right >= left:
            middle = (right + left) // 2
            if nums[middle] > target:
                right = middle - 1
            elif nums[middle] < target:
                left = middle + 1
            else:
                return middle
        return -1

 27. 移除元素 - 力扣(LeetCode)

# 快慢指针
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        slow = 0
        fast = 0
        for i in range(len(nums)):
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow += 1
            fast += 1
        return slow

 977. 有序数组的平方 - 力扣(LeetCode)

# 双指针
class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        k = len(nums) - 1
        left = 0
        right = k
        results = [0] * len(nums)
        while left <= right:
            if nums[left] ** 2 > nums[right] ** 2:
                results[k] = nums[left] ** 2
                left+=1
            else:
                results[k] = nums[right] ** 2
                right-=1
            k-=1
        return results

209. 长度最小的子数组 - 力扣(LeetCode)

# 滑动窗口
class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        left = 0
        right = 0
        nums_lenth = len(nums)
        result_lenth = nums_lenth + 1
        value = 0
        while right < nums_lenth:
            value+=nums[right]
            while value >= target:
                result_lenth = min(result_lenth,right-left+1)
                value-=nums[left]
                left+=1
            right+=1
        if result_lenth!=nums_lenth+1:
            return result_lenth
        return 0

 59. 螺旋矩阵 II - 力扣(LeetCode)

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        result = [[0]*n for _ in range(n)]
        loop,mid = n//2,n//2
        start = 0
        count = 1
        for offset in range(1,loop+1):
            # 左到右
            for j in range(start,n-offset):
                result[start][j] = count
                count+=1
            for j in range(start,n-offset):
                result[j][n-offset] = count
                count+=1
            for j in range(n-offset,start,-1):
                result[n-offset][j] = count
                count+=1
            for j in range(n-offset,start,-1):
                result[j][start] = count
                count+=1
            start+=1
        if n%2 ==1:
            result[mid][mid]=count
        return result

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值