<LeetCode OJ> 27. Remove Element

来源:互联网 发布:restful 接口实例java 编辑:程序博客网 时间:2024/05/28 16:24

27. Remove Element

My Submissions
Total Accepted: 93055 Total Submissions: 285916 Difficulty: Easy

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.



分析:

双指针遍历,cnt始终指向当前元素以前不为val的元素序列

class Solution {public:    int removeElement(vector<int>& nums, int val) {        int cnt=0;        for (int i = 0; i<nums.size(); i++)              if(nums[i] != val)                nums[cnt++] = nums[i];        return cnt;      }};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50448834

原作者博客:http://blog.csdn.net/ebowtang

1 0
原创粉丝点击