Leetcode 283: Move Zeroes

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.For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

题意

考虑一个数组,写一个函数使用该数组的0移动到数组尾部,而非零的数保持一定的顺序。比如,给一个数组nums =[0, 1, 0, 3, 12],经过该函数处理后,nums应该变为[1, 3, 12, 0, 0];

程序(C语言)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void swap(int* nums, int left, int right){
if (left == right){
return;
}else{
nums[left] ^= nums[right];
nums[right] ^= nums[left];
nums[left] ^= nums[right];
}
}
void moveZeroes(int* nums, int numsSize){
int left = 0;
int right = 0;
while (right < numsSize){
if (nums[right] != 0){
swap(nums, left, right);
left++;
}
right++;
}
}

结果

21 / 21 test cases passed.
Status: Accepted
Runtime: 8 ms

分享到 评论