Q8 LeetCode283 移动零

1.使用快慢指针

2.slow指针指向0

3.quick指向可替换的数

4.最后从slow指针开始循环赋0

 

 1 class Solution {
 2     public void moveZeroes(int[] nums) {
 3     int slowIndex=0;
 4     int quickIndex;
 5     for(quickIndex=0;quickIndex<nums.length;quickIndex++){
 6         if(nums[quickIndex]!=0){
 7             nums[slowIndex]=nums[quickIndex];
 8             slowIndex++;
 9         }
10     }
11     for(int i=slowIndex;i<nums.length;i++){
12             nums[i]=0;
13         }   
14     }
15 }

 

posted @ 2024-06-05 22:32  清川1  阅读(13)  评论(0)    收藏  举报