leetcode_27_Remove Element

来源:互联网 发布:飞鹰网络电视手机版g 编辑:程序博客网 时间:2024/06/06 11:02

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢微笑


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 you leave beyond the new length.


//方法二class Solution {public:    int removeElement(vector<int>& nums, int val) {        return distance(nums.begin(), remove(nums.begin(), nums.end(), val));    }};

//方法一:自测Acceptedclass Solution {    public:    int removeElement(int A[], int n, int elem) {int length = 0;for(int i=0; i<n; i++){if(A[i] != elem){A[length] = A[i];length++;}}for(int j=length; j<n; j++)A[j]=0;//for(int i=0; i<n; i++)//cout<<A[i];//cout<<endl;//cout<<length<<endl;return length;    }};


1 0
原创粉丝点击