zoukankan      html  css  js  c++  java
  • Remove Element -- leetcode

    题目描述:
    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.

    代码:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int removeElement(int[], int, int);
    int removeElement(vector<int>&, int);
    int main() {
    
        int a[] = { 1,2,3,3,4,5,3 };
        int ans1 = removeElement(a, 7, 3);
        cout << "ans1= " << ans1 << endl;
    
        vector<int> vec;
        vec.push_back(1);
        vec.push_back(2);
        vec.push_back(3);
        vec.push_back(3);
        vec.push_back(4);
        vec.push_back(5);
        vec.push_back(3);
    
        int ans2 = removeElement(vec, 3);
        cout << "ans2= " << ans2 << endl;
    
    
        system("pause");
        return 0;
    }
    
    //双向指向实现数组元素的移除
    int removeElement(int nums[], int n, int val) {
        int i = 0;
        for (int j = 0; j < n; ++j) {
            if (nums[j] != val) {//数组当前的值不是val
                nums[i] = nums[j];//依次将非val的值重新给nums赋值
                ++i;
            }
        }
        return i;
    }
    
    
    //vector数组的移除
    int removeElement(vector<int>& nums, int val) {
        vector<int>::iterator it = nums.begin();
        while (it != nums.end()) {
            if (*it == val) {
                it = nums.erase(it);
            }
            else {
                ++it;
            }
        }
        return nums.size();
    }

    结果

    补充:
    STL中的vector:增减元素对迭代器的影响
    解答:这个问题主要是针对连续内存容器和非连续内存容器。
    a、对于连续内存容器,如vector、deque等,增减元素均会使得当前之后的所有迭代器失效。因此,以vector删除元素为例:由于erase()总是指向被删除元素的下一个元素的有效迭代器,因此,可以利用该连续内存容器的成员erase()函数的返回值。常见的编程写法为:

    for(auto iter = myvec.begin(); iter != myvec.end())
    {
        if(delete iter)
            iter = myvec.erase(iter);
        else ++iter;
    }

    b、对于非连续内存容器,如set、map等。增减元素只会使得当前迭代器无效。仍以删除元素为例,由于删除元素后,erase()返回的迭代器将是无效的迭代器。因此,需要在调用erase()之前,就使得迭代器指向删除元素的下一个元素。常见的编程写法为:

    for(auto iter = myset.begin(); iter != myset.end())
    {
        if(delete iter)
            myset.erase(iter++); //使用一个后置自增就OKelse ++iter;
    }
  • 相关阅读:
    linux sed
    linux vim
    linux 文本处理
    Xception: Deep Learning with Depthwise Separable Convolutions
    Residual Attention Network for Image Classification
    ResNeXt:Aggregated Residual Transformations for Deep Neural Networks
    Densely Connected Convolutional Networks
    Deep Pyramidal Residual Networks
    SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size
    Wide Residual Networks
  • 原文地址:https://www.cnblogs.com/xiaocai-ios/p/7779753.html
Copyright © 2011-2022 走看看