【题目描述】
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Notice
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
注意事项
1.必须在原数组上操作
2.最小化操作数
【题目链接】
2、利用x遍历数组nums: 3、若nums[x]非0,则交换nums[x]与nums[y],并令y+1 注意: y指针指向首个0元素可能存在的位置 遍历过程中,算法确保[y, x)范围内的元素均为0 【参考答案】 www.jiuzhang.com/solutions/move-zeroes/
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦