鱼C论坛

 找回密码
 立即注册
查看: 766|回复: 6

[技术交流] 【朱迪的LeetCode刷题笔记】】283. Move Zeroes #Easy #Python

[复制链接]
发表于 2023-6-5 10:12:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Judie 于 2023-6-4 21:33 编辑

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]

Example 2:
Input: nums = [0]
Output: [0]

Constraints:
1 <= nums.length <= 104
-231 <= nums <= 231 - 1

Follow up:
Could you minimize the total number of operations done?

Judy
Python beats 28.67%
  1. class Solution(object):
  2.     def moveZeroes(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: None Do not return anything, modify nums in-place instead.
  6.         """
  7.         x = nums.count(0)
  8.         for i in range(x):
  9.             nums.remove(0)
  10.         nums.extend([0] * x)
复制代码

Python beats 18.76%
  1. class Solution(object):
  2.     def moveZeroes(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: None Do not return anything, modify nums in-place instead.
  6.         """
  7.         for x in nums:
  8.             if x == 0:
  9.                 nums.remove(0)
  10.                 nums.append(0)
复制代码


Sol1
Python beats 76.93%
  1. class Solution(object):
  2.     def moveZeroes(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: None Do not return anything, modify nums in-place instead.
  6.         """
  7.         count=nums.count(0)
  8.         nums[:]=[i for i in nums if i != 0]
  9.         nums+=[0]*count
  10.                
复制代码


Sol2
Python beats 59.6% #TwoPointers
  1. class Solution(object):
  2.     def moveZeroes(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: None Do not return anything, modify nums in-place instead.
  6.         """
  7.         slow = 0
  8.         for fast in range(len(nums)):
  9.             if nums[fast] != 0 and nums[slow] == 0:
  10.                 nums[slow], nums[fast] = nums[fast], nums[slow]

  11.             # wait while we find a non-zero element to
  12.             # swap with you
  13.             if nums[slow] != 0:
  14.                 slow += 1
复制代码


Sol3
Python beats 87.64% (better than Sol2!) #TwoPointers
  1. class Solution(object):
  2.     def moveZeroes(self, nums):
  3.         n = len(nums)
  4.         i = 0
  5.         for j in range(n):
  6.             if (nums[j] != 0):
  7.                 nums[i], nums[j] = nums[j], nums[i]
  8.                 i += 1
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-6-7 08:05:29 | 显示全部楼层
我写过这题hh
  1. class Solution:
  2.     def moveZeroes(self, nums: List[int]) -> None:
  3.         """
  4.         Do not return anything, modify nums in-place instead.
  5.         """
  6.         n = len(nums)
  7.         i = 0
  8.         j = 0
  9.         while j < n:
  10.             if nums[j] != 0:
  11.                 nums[i] , nums[j] = nums[j] , nums[i]
  12.                 i += 1
  13.             j += 1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-7 08:10:11 | 显示全部楼层
还有这个,这是我第一次写的,投机取巧了,上面那个是看了答案有双指针再去写的
  1. class Solution:
  2.     def moveZeroes(self, nums: List[int]) -> None:
  3.         """
  4.         Do not return anything, modify nums in-place instead.
  5.         """
  6.         a = nums.count(0)
  7.         for i in range(a):
  8.             nums.remove(0)
  9.             nums.append(0)
复制代码
001.png

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-7 21:43:36 | 显示全部楼层
yinda_peng 发表于 2023-6-6 19:10
还有这个,这是我第一次写的,投机取巧了,上面那个是看了答案有双指针再去写的

hhhh 我也这么写了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-7 21:47:29 | 显示全部楼层

这个你可以展开讲讲吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-8 08:52:24 | 显示全部楼层
Judie 发表于 2023-6-7 21:47
这个你可以展开讲讲吗

差不多就是跟答案那个双指针一样的,i表示当前已经处理好的非0元素的最后一位,j表示当前处理到的位置。如果nums[j]不等于0,就将其交换到nums的位置上,然后i向后移动一位,继续处理下一个元素
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-8 20:50:44 | 显示全部楼层
yinda_peng 发表于 2023-6-7 19:52
差不多就是跟答案那个双指针一样的,i表示当前已经处理好的非0元素的最后一位,j表示当前处理到的位置。 ...

噢噢噢噢 明白了 谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 12:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表