Remove Element:

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.

这道题如果没有其他限制,另开一条数组,将不等于val的数存进去就ok了,但是它限制了我们另开一条数组,所以可以用c++ STL中vector的erase函数直接在原数组中更改:

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
while(remove(nums,val)){
}
return nums.size();
}
bool remove(vector<int>& nums, int val){
vector<int>::iterator iter;
for(iter=nums.begin();iter!=nums.end();iter++){
if(*iter==val){
nums.erase(iter);
return true;
}
}
return false;
}
};

而在一些语言中没有erase函数可以用,这么办?不用erase函数的话,我们可以这样处理:

int removeElement(vector<int>& nums, int val) {
int cnt = 0;
for(int i = 0 ; i < nums.size() ; ++i) {
if(nums[i] == val)
cnt++;
else
nums[i-cnt] = nums[i];
}
return nums.size()-cnt;
}

或者

int removeElement(int A[], int n, int elem) {
int begin=0;
for(int i=0;i<n;i++){
if(A[i]!=elem){
A[begin++]=A[i];
}
}
return begin;
}

[LeetCode] Remove Element题解的更多相关文章

  1. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  2. LeetCode Remove Element

    原题链接在这里:https://leetcode.com/problems/remove-element/ 题目: Given an array and a value, remove all ins ...

  3. [LeetCode] Remove Element 移除元素

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

  4. [LeetCode] Remove Element (三种解法)

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

  5. LeetCode——Remove Element

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

  6. [Leetcode] remove element 删除元素

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

  7. leetcode Remove Element python

    class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] ...

  8. LeetCode Remove Element删除元素

    class Solution { public: int removeElement(int A[], int n, int elem) { ]; int i,num=n; ;i<n;i++){ ...

  9. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

随机推荐

  1. 从Oracle官网学习oracle数据库和java

    网上搜索Oracle官网:oracle官网 进入Oracle官网 点击menu-Documentation-Java/Database,进入Oracle官网的文档网站 首先是Java,可以看到Java ...

  2. 使用textarea标签代替input标签可以实现输入框的大小调节,自动换行,滚动条显示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 一篇文章搞懂Linux安全!

    Linux是开放源代码的免费正版软件,同时也是因为较之微软的Windows NT网络操作系统而言,Linux系统具有更好的稳定性.效率性和安全性. 在Internet/Intranet的大量应用中,网 ...

  4. scrapy之 downloader middleware

    一. 功能说明 Downloader Middleware有三个核心的方法 process_request(request, spider) process_response(request, res ...

  5. RHEL配置本地yum

    RHEL(即Red Hat Enterprise Linux的缩写)配置本地yum 提前将 rhel-server-6.7-x86_64-dvd.iso 文件上传到服务器上 1.在根目录创建文件夹/m ...

  6. 根据现有PDF模板填充信息(SpringBoot)

    根据现有PDF模板填充信息(SpringBoot+maven) 首先得有一个pdf模板,建立pdf模板需要下载工具 红色框为文本框,filename为域名.java需要根据域名赋值 pom 文件配置 ...

  7. 3. STL编程三

    1. 算法容器的使用: #include <iostream> #include <functional> #include <vector> #include & ...

  8. JS设计模式之单体模式(Singleton)

    单体模式作为一种软件开发模式在众多面向对象语言中得到了广泛的使用,在javascript中,单体模式也是使用非常广泛的,但是由于javascript语言拥有其独特的面向对象方式,导致其和一些传统面向对 ...

  9. Eclipse 的SVN 的分支

    分支 概念 在版本控制过程中,使用多个分支同时推进多个不同功能开发.   不使用分支开发:人与人之间协作   使用分支开发:小组和小组之间协作 作用  多个功能开发齐头并进同时进行 任何一个分支上功能 ...

  10. 本地docker镜像上传到腾讯云镜像仓库。

    和上篇上传到Docker Hub类似,只是登录时切换成腾讯云镜像仓库地址(ccr.ccs.tencentyun.com/longdbtencentdocker/publongdb)即可. docker ...