No.27 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.
Tags: Array of pointers
Move all the given numbers in the divisor group
Requirements: Remove in situ, no extra space, return the length of the new array, change the original order
A typical two-pointer
1#include"stdafx.h"2#include <map>3#include <vector>4#include <iostream>5 using namespacestd;6 7 classSolution8 {9 Public:Ten intRemoveelement (vector<int> &nums,intval) One{//move all the given numbers in the divisor group A //requirements: Remove in situ, no extra space, return the length of the new array, change the original order - intSize =nums.size (); - intCount =0;//count the number of occurrences of the current Val the - for(intI=0; i<size; i++) - { - if(Nums[i] = =val) +count++; - Else + { A if(Count! =0) atNums[i-count] =Nums[i]; - } - } -Nums.erase (Nums.begin () +size-count,nums.end ()); - returnsize-count; - } in }; - to intMain () + { - solution Sol; the intData[] = {1,2,2,3,3,2,1,7,9,Ten}; *vector<int> Test (data,data+sizeof(data)/sizeof(int)); $ for(ConstAuto &i:test)Panax Notoginsengcout << i <<" "; -cout <<Endl; thecout<< boolalpha << sol.removeelement (test,2) <<Endl; + for(ConstAuto &i:test) Acout << i <<" "; thecout <<Endl; +}
No.27 Remove Element