移除元素

文章目录

移除元素


双指针
删除指定项且不改变顺序

py 复制代码
def removeElement(nums: list[int], val: int) -> int:
    fast = slow = 0
    while fast < len(nums):
        if nums[fast] != val:
            nums[slow] = nums[fast]
            slow += 1
        # 当 fast 指针遇到要删除的元素时停止赋值
        # slow 指针停止移动, fast 指针继续前进
        fast += 1
    return slow
nums = [0,1,2,2,3,0,4,2,2,2]
val = 2
print(removeElement(nums,val))

删除有序数组中的重复项


删除重复项且不改变顺序

py 复制代码
def removeDuplicates(nums: list[int]) -> int:
    slow = 0
    for fast in range(len(nums)):
        if fast == 0:
            nums[slow] = nums[fast]
            slow += 1
        if fast >= 1:
            if nums[fast] != nums[fast - 1]:
                nums[slow] = nums[fast]
                slow += 1
    return slow

nums = [1,1,2]
print(removeDuplicates(nums))

或者:

py 复制代码
def removeDuplicates(nums: list[int]) -> int:
    for i in range(len(nums) - 1, 0, -1):
        if nums[i] == nums[i - 1]:
            nums.pop(i)
    return len(nums)

nums = [0,0,1,1,1,2,2,3,3,4]
print(removeDuplicates(nums))

移动零


和题一一样操作

py 复制代码
def moveZeroes(nums: list[int]) -> None:
    """
    Do not return anything, modify nums in-place instead.
    """
    slow, fast = 0, 0
    while fast < len(nums):
        if nums[fast] != 0:
            nums[slow] = nums[fast]
            slow += 1
        fast += 1
    for i in range(slow,len(nums)):
        nums[i] = 0
    return nums

nums = [0,1,0,3,12]
print(moveZeroes(nums))

比较含退格的字符串

想不到

python 复制代码
def backspaceCompare(s: str, t:str) -> bool:
    i, j = len(s) - 1, len(t) - 1
    skipS, skipT = 0, 0
    while i >= 0 or j >= 0:
        # S循环
        while i >= 0:
            if s[i] == '#':
                skipS += 1
                i -= 1
            elif skipS > 0:
                skipS -= 1
                i -= 1
            else:
                break
        # T循环
        while j >= 0:
            if t[j] == '#':
                skipT += 1
                j -= 1
            elif skipT > 0:
                skipT -= 1
                j -= 1
            else:
                break
        a = "" if i < 0 else s[i] # a[-1] = a,所以不能有负索引
        b = "" if j < 0 else t[j]
        if a != b:
            return False
        i -= 1
        j -= 1
    return True


s = "aaa###a"
t = "aaaa###a"
print(backspaceCompare(s,t))

有序数组的平方


正负数平方排序

py 复制代码
def sortedSquares(nums: list[int]) -> list[int]:
    for i in range(len(nums)):
        nums[i] = nums[i] * nums[i]
    nums.sort()
    return nums
nums = [-4,-1,0,3,10]
print(sortedSquares(nums))

或者:

py 复制代码
def sortedSquares(nums: list[int]) -> list[int]:
    left = 0
    right = len(nums) - 1
    temp_lis = [-1] * len(nums)
    site = len(nums) - 1
    while left <= right:
        if nums[left] ** 2 < nums[right] ** 2:
            temp_lis[site] = nums[right] ** 2
            right -= 1
        else:
            temp_lis[site] = nums[left] ** 2
            left += 1
        site -= 1
    return temp_lis
nums = [-4,-1,0,3,10]
print(sortedSquares(nums))
相关推荐
忘却的旋律dw21 分钟前
使用LLM模型的tokenizer报错AttributeError: ‘dict‘ object has no attribute ‘model_type‘
人工智能·pytorch·python
20岁30年经验的码农30 分钟前
Java RabbitMQ 实战指南
java·开发语言·python
非著名架构师36 分钟前
极端天气下的供应链韧性:制造企业如何构建气象风险防御体系
大数据·人工智能·算法·制造·疾风气象大模型·风光功率预测
星轨初途44 分钟前
数据结构排序算法详解(2)——选择排序(附动图)
c语言·数据结构·经验分享·笔记·b树·算法·排序算法
kaikaile19952 小时前
基于 MATLAB 的室内三维定位
算法
studytosky2 小时前
深度学习理论与实战:MNIST 手写数字分类实战
人工智能·pytorch·python·深度学习·机器学习·分类·matplotlib
AGI前沿2 小时前
AdamW的继任者?AdamHD让LLM训练提速15%,性能提升4.7%,显存再省30%
人工智能·算法·语言模型·aigc
Tan_Ying_Y2 小时前
什么是垃圾回收算法 他的底层原理是什么?
算法
Xの哲學2 小时前
Linux 分区表深度技术剖析
linux·网络·算法·架构·边缘计算
写写闲篇儿2 小时前
经典算法题剖析之传递信息(三)
算法