Skip to content

Commit 8990981

Browse files
add solution to move zeroes leetcode problem (#126)
* add solution to move zeroes lc problem * update readme (add profile links to contributors list)
1 parent 90e854c commit 8990981

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

C++/Move-Zeroes.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
//Problem Statement: 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.
3+
4+
//Solution:
5+
class Solution {
6+
public:
7+
void moveZeroes(vector<int>& nums) {
8+
int i = 0;
9+
for (int j = 0; j < nums.size(); j++) {
10+
// Bring all non zero numbers to the start of the array
11+
if (nums[j] != 0) {
12+
nums[i++] = nums[j];
13+
}
14+
}
15+
16+
while(i<nums.size()){
17+
nums[i++] = 0;
18+
}
19+
}
20+
};
21+
22+
// Time Complexity: O(n)
23+
// Space Complexity: O(n)

0 commit comments

Comments
 (0)