Facebook Phone Interview -- Move Zeros to Right (Easy)
来源:互联网 发布:线割编程招聘 编辑:程序博客网 时间:2024/03/28 18:08
March 22th, 2016.
Move Zeros to the right end but keep the order of non-zero numbers.
#include <stdio.h>#inlcude <iostream>#include <vector>using namespace std;// move Zeros to the right end of the array.// classical two pointers problem.void moveZeroToRight(vector<int>& array) { if(array.size() <= 1) return; // if the array only has 0-1 values, no need to sort. int left = 0; // one pointer to remember the left copy. int i = 0; // one pointer to loop the array. while(i < array.size()) { if(array[i] != 0) { array[left++] = array[i++]; // if it is not zero, copy it to the left end. } else { i++; // otherwise, i pointer keeps on moving. } } // copy all non-zeros to the left now. But we need to remember to set the left values to 0. while(left < array.size()) { array[left++] = 0; }}// the following is for test purpose.void printArray(vector<int> array) { for(int i = 0; i < array.size(); ++i) { printf("%d\n", array[i]); }}int main(void) { vector<int> array; array.push_back(0); array.push_back(1); moveZeroToRight(array); printArray(array);}
0 0
- Facebook Phone Interview -- Move Zeros to Right (Easy)
- Facebook Phone Interview: 3, 4 Sum (Easy)
- 283.move to zeros
- Facebook Phone Interview: Phone Number to Letter Combinations
- LeetCode笔记:Move Zeros[Difficulty: Easy]
- move zeros
- Move Zeros
- Move Zeros
- 【leetcode】283. Move Zeros
- Leetcode Move Zeros
- (java)leetcode Move Zeros
- move zeros leetcode
- 【leetcode】:move zeros
- leetcode Move Zeros
- leetcode之move zeros
- Leetcode283 Move Zeros
- leetcode之 Move Zeros
- leetcode:Move Zeros
- 构造方法
- Spring源码之bean的加载(一)
- Spring源码之bean的加载(二)获取单例bean
- Spring源码之bean的加载(三)从bean中获取对象
- 解决DRM找不到的问题。
- Facebook Phone Interview -- Move Zeros to Right (Easy)
- 嵌入式编程基础知识学习(1)
- Spring源码之bean的加载(四)获取单例
- 并查集
- Spring源码之bean的加载(五)准备创建bean
- 【杭电oj】2277 - Change the ball(找规律)
- Using multiple LLVM versions on Ubuntu
- 【BZOJ 4443】 [Scoi2015]小凸玩矩阵|二分|最大匹配|匈牙利
- Java命名规范及给自己写的方法添加注解