adding iterator exercise
This commit is contained in:
21
leetcode/p283_movezeroes.cpp
Normal file
21
leetcode/p283_movezeroes.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
class Solution {
|
||||
public:
|
||||
void moveZeroes(vector<int>& nums) {
|
||||
vector<int>::iterator itr = nums.begin();
|
||||
// use itr2 to represent the "new" vector
|
||||
vector<int>::iterator itr2 = nums.begin();
|
||||
while(itr != nums.end()){
|
||||
// traverse the vector and store all non-zero elements in the "new" vector.
|
||||
if(*itr != 0){
|
||||
*itr2 = *itr;
|
||||
itr2++;
|
||||
}
|
||||
itr++;
|
||||
}
|
||||
// as we skipped zeroes above, we now fill them in.
|
||||
while(itr2!=nums.end()){
|
||||
*itr2 = 0;
|
||||
itr2++;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user