[Leetcode 283]Move Zeros

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Input: nums = [0]
Output: [0]
  • 1 <= nums.length <= 104
  • -231 <= nums[i] <= 231 - 1
void swap(int* a, int* b){
if(a == b)
return;
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
void moveZeroes(int* nums, int numsSize){
int write_idx = 0;
for(int i=0; i<numsSize; i++){
if(nums[i] != 0){
swap(&nums[write_idx], &nums[i]);
write_idx++;
}
}
}

--

--

--

Hi I am Samuel

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is Web 3.0 and Why you should care about it?

How the TOMRA Field Potato Sorter Improves your Sort

Iron Horse | What is Developer Relations?

Install RStudio Shiny Server on Centos 7

How to Monitor Nginx using Netdata on CentOS 7

gRPC Adoption and Working Architecture

Git Commands Every Developer Should Know — Part 1

PYTHON: UNIT TESTING

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Samuel Liu

Samuel Liu

Hi I am Samuel

More from Medium

Recursive functions

Sorting Techniques 2.0

String Hashing (MondayAlgo #8)

Should I make class member variables const or constexpr?