当前位置: 首页> 财经> 创投人物 > 大连网页设计公司_进口外贸网站有哪些_成都网站排名 生客seo_搜狗seo查询

大连网页设计公司_进口外贸网站有哪些_成都网站排名 生客seo_搜狗seo查询

时间:2025/9/2 4:53:56来源:https://blog.csdn.net/weixin_44245188/article/details/142633656 浏览次数:0次
大连网页设计公司_进口外贸网站有哪些_成都网站排名 生客seo_搜狗seo查询

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[i] <= 231 - 1

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

链接:
https://leetcode.cn/problems/move-zeroes/description/
思路:
设置快慢指针,快指针找到第一个非零元素,慢指针找到第一个零元素,交换位置即可。

class Solution:def moveZeroes(self, nums: List[int]) -> None:slow,fast = 0,0while fast < len(nums):while nums[slow] !=0 and slow < len(nums)-1:slow += 1fast += 1while nums[fast] == 0 and  fast < len(nums)-1:fast += 1nums[slow],nums[fast] = nums[fast],nums[slow]slow += 1fast += 1
关键字:大连网页设计公司_进口外贸网站有哪些_成都网站排名 生客seo_搜狗seo查询

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: