《代码随想录》刷题记录

https://github.com/youngyangyang04/leetcode-master/tree/master

数组:704.二分查找

数组 分治

递归版本,实际上可以写迭代版本的

python 复制代码
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        return self.binary_search(nums, 0, len(nums) - 1, target)

    def binary_search(self, nums, left, right, target):
        if left > right or right < left:
            return -1
        mid = (left + right) // 2
        if target == nums[mid]:
            return mid
        elif target < nums[mid]:
            return self.binary_search(nums, left, mid - 1, target)
        else:
            return self.binary_search(nums, mid + 1, right, target)

数组:27.移除元素

数组 双指针

python 复制代码
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        if not nums:
            return 0
        left, right = 0, len(nums) - 1
        if left == right:
            return left - (nums[left] == val) + 1
        while left < right:
            if nums[left] !=val:
                left += 1
            elif nums[right] == val:
                right -= 1
            else:
                nums[left], nums[right] = nums[right], nums[left]
                left += 1
                right -= 1
        return left - (nums[left] == val) + 1

数组:977.有序数组的平方

数组 双指针

python 复制代码
class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        # 若所有为正数,正序平方返回,负数,逆序平方返回
        # 正负均有,找到边界,对比绝对值返回
        if len(nums) == 1:
            return [nums[0] * nums[0]]

        if nums[0] <= 0 and nums[-1] <= 0:
            res = []
            for i in range(len(nums) - 1, -1, -1):
                res.append(nums[i] * nums[i])
            return res
        elif nums[0] >= 0 and nums[-1] >= 0:
            return [n * n for n in nums]
        else:
            i = 0
            while i < len(nums):
                if nums[i] >= 0:
                    i -= 1
                    break
                i += 1
            j = i + 1

            res = []
            while i >= 0 or j < len(nums):
                if i >= 0 and j < len(nums):
                    if nums[i] * nums[i] > nums[j] * nums[j]:
                        res.append(nums[j] * nums[j])
                        j += 1
                    else:
                        res.append(nums[i] * nums[i])
                        i -= 1
                elif i >= 0:
                    res.append(nums[i] * nums[i])
                    i -= 1
                else:
                    res.append(nums[j] * nums[j])
                    j += 1
            return res

数组:209.长度最小的子数组

数组 滑动窗口

python 复制代码
class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        l = len(nums) + 1
        sumary = nums[0]
        left = right = 0
        while -1 < left <= right < len(nums):
            if sumary >= target:
                l = min(l, right - left + 1)
                left += 1
                if left <= right:
                    sumary -= nums[left - 1]
                else:
                    break
            else:
                right += 1
                if right < len(nums):
                    sumary += nums[right]
                else:
                    break
        if l > len(nums):
            return 0
        else:
            return l

数组:区间和

前缀和

python 复制代码
n = 0
nums = []
questions = []

try:
    n = int(input())
    for _ in range(n):
        nums.append(int(input()))
    while True:
        r = input().split(" ")
        questions.append((int(r[0]), int(r[1])))

except EOFError:
    pass

prefix_sum = [0] * len(nums)
for i in range(1, len(nums)):
    prefix_sum[i] = prefix_sum[i - 1] + nums[i - 1]

for a, b in questions:
    print(prefix_sum[b] - prefix_sum[a] + nums[b])

数组:开发商购买土地

数组 前缀和

数组:螺旋矩阵

数组 模拟

特别注意矩形情况最内圈的边界条件

python 复制代码
class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        res = []
        m, n = len(matrix), len(matrix[0])
        c = min(math.ceil(m / 2), math.ceil(n / 2))
        for i in range(c):
            x, y = i, i - 1

            y += 1
            if not (y < n - i):
                return res
            while y < n - i:
                res.append(matrix[x][y])
                y += 1
            y -= 1

            x += 1
            if not (x < m - i):
                return res
            while x < m - i:
                res.append(matrix[x][y])
                x += 1
            x -= 1

            y -= 1
            if not (y > i - 1):
                return res
            while y > i - 1:
                res.append(matrix[x][y])
                y -= 1
            y += 1

            x -= 1
            if not (x > i):
                return res
            while x > i:
                res.append(matrix[x][y])
                x -= 1
        return res
相关推荐
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark1 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her11 天前
并查集-听课笔记
笔记·算法·并查集
码流子1 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven1 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark1 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218611 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法