当前位置:网站首页>[leetcode] 283 move zero
[leetcode] 283 move zero
2022-07-02 15:36:00 【Crisp ~】
Given an array nums, Write a function that will 0 Move to end of array , While maintaining the relative order of non-zero elements .
Please note that , You must operate on the array in place without copying it .
Example 1:
Input : nums = [0,1,0,3,12]
Output : [1,3,12,0,0]
Example 2:
Input : nums = [0]
Output : [0]
Tips :
- 1 <= nums.length <= 104
- -231 <= nums[i] <= 231 - 1
# Stupid method
class Solution(object):
def moveZeroes(self, nums):
n = len(nums)
for i in range(n):
if nums[n-i-1]==0:
tmp = nums.pop(n-i-1)
nums.append(tmp)
# Double pointer , Speed pointer , Fast pointer fast traversal sequence value , Encountered a non-zero number covering the slow pointer position
class Solution(object):
def moveZeroes(self, nums):
length = len(nums)
slow = 0
for fast in range(length):
if nums[fast]:
nums[slow] = nums[fast]
slow+=1
nums[slow:]=(length-slow)*[0]
边栏推荐
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- Case introduction and problem analysis of microservice
- 02.面向容器化后,必须面对golang
- 11_ Redis_ Hyperloglog_ command
- 语义分割学习笔记(一)
- 6.12 critical moment of Unified Process Platform
- Bing. Com website
- 5. Practice: jctree implements the annotation processor at compile time
- 2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
猜你喜欢
How to find a sense of career direction
LeetCode刷题——递增的三元子序列#334#Medium
List set & UML diagram
How to intercept the value of a key from the JSON string returned by wechat?
Mavn builds nexus private server
【LeetCode】1162-地图分析
Party History Documentary theme public welfare digital cultural and creative products officially launched
20_Redis_哨兵模式
MySQL calculate n-day retention rate
03. Preliminary use of golang
随机推荐
03_線性錶_鏈錶
17_ Redis_ Redis publish subscription
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Oracle primary key auto increment
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
How to write sensor data into computer database
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
14_Redis_乐观锁
Yolo format data set processing (XML to txt)
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
Engineer evaluation | rk3568 development board hands-on test
18_Redis_Redis主从复制&&集群搭建
损失函数与正负样本分配:YOLO系列
11_Redis_Hyperloglog_命令
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
高考分数线爬取
【网络安全】网络资产收集
Custom exception
LeetCode刷题——递增的三元子序列#334#Medium
[network security] network asset collection