移除元素

文章目录

移除元素


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

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))
相关推荐
曲幽28 分钟前
我用了FastApiAdmin后,连夜把踩过的坑都整理出来了
redis·python·postgresql·vue3·fastapi·web·sqlalchemy·admin·fastapiadmin
心中有国也有家2 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
前端若水2 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
绝知此事2 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
碧海银沙音频科技研究院2 小时前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
涛声依旧-底层原理研究所3 小时前
残差连接与层归一化通俗易懂的详解
人工智能·python·神经网络·transformer
csdn_aspnet3 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
fantasy_arch3 小时前
pytorch人脸匹配模型
人工智能·pytorch·python
熊猫_豆豆3 小时前
广义相对论水星近日点进动完整详细数学推导
python·天体·广义相对论