#include <stdio.h> | |||||
void moveZeroes(int*, int); | |||||
void moveZeroes(int* nums, int numsSize){ | |||||
int numZeroes = 0; | |||||
for (int i = 0; i < numsSize; i++) { | |||||
if (nums[i] == 0) { | |||||
numZeroes++; | |||||
} else { | |||||
nums[i - numZeroes] = nums[i]; | |||||
} | |||||
} | |||||
for (int i = 0; i < numZeroes; i++) { | |||||
nums[numsSize - 1 - i] = 0; | |||||
} | |||||
} | |||||
int main() { | |||||
int nums[5] = {0, 1, 0, 3, 12}; | |||||
int expectedResult[5] = {1, 3, 12, 0, 0}; | |||||
printf("Expected: "); | |||||
for (int i = 0; i < 5; i++) { | |||||
printf("%d ", expectedResult[i]); | |||||
} | |||||
printf("\n"); | |||||
moveZeroes(nums, 5); | |||||
printf("Got: "); | |||||
for (int i = 0; i < 5; i++) { | |||||
printf("%d ", nums[i]); | |||||
} | |||||
printf("\n"); | |||||
} |
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. | |||||
Example: | |||||
Input: [0,1,0,3,12] | |||||
Output: [1,3,12,0,0] | |||||
Note: | |||||
You must do this in-place without making a copy of the array. | |||||
Minimize the total number of operations. | |||||
#!/bin/bash | |||||
gcc -o main main.c | |||||
./main | |||||
rm main |
- python3 (preferably >3.8) | - python3 (preferably >3.8) | ||||
- rustc | - rustc | ||||
- gcc | - gcc | ||||
- SQlite | |||||
Then you can see all the problems go through an example case with: | Then you can see all the problems go through an example case with: | ||||