leetcode原地删除系列-283移除0


/**
<p>给定一个数组 <code>nums</code>,编写一个函数将所有 <code>0</code> 移动到数组的末尾,同时保持非零元素的相对顺序。</p>

<p><strong>请注意</strong>&nbsp;,必须在不复制数组的情况下原地对数组进行操作。</p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong> nums = <code>[0,1,0,3,12]</code>
<strong>输出:</strong> <code>[1,3,12,0,0]</code>
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong> nums = <code>[0]</code>
<strong>输出:</strong> <code>[0]</code></pre>

<p>&nbsp;</p>

<p><strong>提示</strong>:</p>
<meta charset="UTF-8" />

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-2<sup>31</sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li>
</ul>

<p>&nbsp;</p>

<p><b>进阶:</b>你能尽量减少完成的操作次数吗?</p>
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div><br><div><li>👍 1525</li><li>👎 0</li></div>
*/

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public void moveZeroes(int[] nums) {
        if(nums.length==0){
            return ;
        }
        int fast=0,slow=0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[fast]!=0){
                nums[slow]=nums[fast];
                slow++;
            }
            fast++;
        }
        for (int i = slow; i <nums.length ; i++) {
            nums[i]=0;
        }
        return;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

posted @ 2022-04-06 14:45  小傻孩丶儿  阅读(28)  评论(0编辑  收藏  举报