LeetCode27.RemoveElement数组元素删除

27. Remove Element

成都创新互联基于分布式IDC数据中心构建的平台为众多户提供成都移动机房 四川大带宽租用 成都机柜租用 成都服务器租用。

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

题目大意:删除容器中指定的重复元素,然后返回容器的长度。要求不能申请数组来处理。

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

2016-08-05 20:42:00


网站题目:LeetCode27.RemoveElement数组元素删除
当前链接:http://bzwzjz.com/article/gogceg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 自适应网站建设 成都网站设计 成都网站建设公司 重庆网站建设 H5网站制作 重庆网站建设 宜宾网站设计 成都网站建设 成都网站建设 成都网站制作 成都网站制作 成都网站设计 成都商城网站制作 温江网站设计 重庆网站设计 企业网站设计 手机网站建设 网站制作 盐亭网站设计 成都网站建设 温江网站设计 成都网站建设