Move Zeroes Solutions in C++
Number 283
Difficulty Easy
Acceptance 57.9%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/move-zeroes/// Author : Calinescu Valentin, Hao Chen// Date : 2015-10-21class Solution {public:/** Solution (Calinescu Valentin)* ==============================** One solution would be to store the position of the next non-zero element of the array.* Every position of the array, starting with position 0, must store this next non-zero* element until we can no more do that, case in which we need to add the remaining zeros* that we skipped.*** Time Complexity: O(N)* Space Complexity: O(1)**/void moveZeroes(vector<int>& nums) {int i = 0, poz = 0;for(i = 0; i < nums.size() && poz < nums.size(); i++){while(poz < nums.size() && nums[poz] == 0)poz++;if(poz < nums.size())nums[i] = nums[poz];elsei--; // we need 0 on position i, but i is increasing one last timepoz++;}for(; i < nums.size(); i++)nums[i] = 0;}/** Another implemtation which is easy to understand (Hao Chen)* ===========================================================** We have two pointers to travel the array, assume they named `p1` and `p2`.** 1) `p1` points the tail of current arrays without any ZEROs.* 2) `p2` points the head of the rest array which skips the ZEROs.** Then we can just simply move the item from `p2` to `p1`.**/void moveZeroes(vector<int>& nums) {int p1=0, p2=0;// Find the first ZERO, where is the tail of the array.// (Notes: we can simply remove this!)for (; p1<nums.size() && nums[p1]!=0; p1++);// copy the item from p2 to p1, and skip the ZEROfor (p2=p1; p2<nums.size(); p2++) {if ( nums[p2] == 0 ) continue;nums[p1++] = nums[p2];}//set ZERO for rest itemswhile ( p1<nums.size() ) nums[p1++] = 0;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09#include <iostream>#include <vector>using namespace std;// Time Complexity: O(n)// Space Complexity: O(n)class Solution {public:void moveZeroes(vector<int>& nums) {vector<int> nonZeroElements;// put all the non zero elements into a new vectorfor(int i = 0 ; i < nums.size() ; i ++)if(nums[i])nonZeroElements.push_back(nums[i]);// make nums[0...nonZeroElements.size()) all non zero elementsfor(int i = 0 ; i < nonZeroElements.size() ; i ++)nums[i] = nonZeroElements[i];// make nums[nonZeroElements.size()...nums.size()) all zero elementsfor(int i = nonZeroElements.size() ; i < nums.size() ; i ++)nums[i] = 0;}};void printVec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {int arr[] = {0, 1, 0, 3, 12};vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));Solution().moveZeroes(vec);printVec(vec);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09#include <iostream>#include <vector>using namespace std;// Time Complexity: O(n)// Space Complexity: O(1)class Solution {public:void moveZeroes(vector<int>& nums) {int k = 0; // keep nums[0...k) are all zero elementsfor(int i = 0 ; i < nums.size() ; i ++ )if(nums[i])nums[k++] = nums[i];// make the nums[k...end) zerosfor(int i = k ; i < nums.size() ; i ++)nums[i] = 0;}};void printVec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {int arr[] = {0, 1, 0, 3, 12};vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));Solution().moveZeroes(vec);printVec(vec);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09#include <iostream>#include <vector>using namespace std;// Time Complexity: O(n)// Space Complexity: O(1)class Solution {public:void moveZeroes(vector<int>& nums) {int k = 0; // keep nums[0...k) are all zero elementsfor(int i = 0 ; i < nums.size() ; i ++)if(nums[i])swap(nums[k++] , nums[i]);}};void printVec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {int arr[] = {0, 1, 0, 3, 12};vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));Solution().moveZeroes(vec);printVec(vec);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09#include <iostream>#include <vector>using namespace std;// Time Complexity: O(n)// Space Complexity: O(1)class Solution {public:void moveZeroes(vector<int>& nums) {int k = 0; // keep nums[0...k) are all zero elementsfor(int i = 0 ; i < nums.size() ; i ++ )if(nums[i])// avoid self swappingif(k != i)swap(nums[k++], nums[i]);elsek ++;}};void printVec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {int arr[] = {0, 1, 0, 3, 12};vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));Solution().moveZeroes(vec);printVec(vec);return 0;}