lhl
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
  • python

  • leetcode

    • 数组

      • 双指针

        • 15. 三数之和
        • 19. 删除链表的倒数第 N 个结点
      • 1.两数相加
      • 11. 盛最多水的容器
      • 26.删除有序数组中的重复项
      • 75. 颜色分类
      • 136.只出现一次的数字
      • 283.移动零
      • 448. 找到所有数组中消失的数字
      • 617. 合并二叉树
    • 位运算

    • 动态规划

    • 链表

    • 栈

    • 树

  • 软件测试

  • Git

  • linux

  • 产品

  • MySql

  • docker

  • leetcode
  • array
2023-04-29

移动零

https://leetcode-cn.com/problems/move-zeroes/ (opens new window) 需要注意的点

  1. python 没有自加,https://www.runoob.com/note/24457 (opens new window), https://www.runoob.com/python/python-func-id.html (opens new window)
  2. 该题用到了快慢指针
class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        index = self.removeElement(nums, 0)
        # 将数组index后面的元素赋0
        while index < len(nums):
            nums[index] = 0
            index += 1
        return nums


        
    def removeElement(self, nums, val):
        """
            快慢指针, 快指针总是在增加,
            慢指针只有在符合条件的时候才增加
        """

        fast = 0
        slow = 0
        while fast < len(nums):
            # slow 符合条件才增加,该题条件是 当fast != val
            if nums[fast] != val:
                nums[slow] = nums[fast]
                slow += 1
            
            # fast 总是增加
            fast += 1

        return slow
class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        # index = self.removeElement(nums, 0)
        fast = 0
        slow = 0
        while fast < len(nums):
            # slow 符合条件才增加,该题条件是 当fast != val
            if nums[fast] != 0:
                nums[slow] = nums[fast]
                slow += 1
            
            # fast 总是增加
            fast += 1

        while slow < len(nums):
            nums[slow] = 0
            slow += 1
        return nums


        
    def removeElement(self, nums, val):
        """
            快慢指针, 快指针总是在增加,
            慢指针只有在符合条件的时候才增加
        """
        pass
       
136.只出现一次的数字
448. 找到所有数组中消失的数字

← 136.只出现一次的数字 448. 找到所有数组中消失的数字→

最近更新
01
lhl learn notes
02
filter
06-09
03
decorator
06-09
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式