题目

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(int A[], int n, int elem) {
if (n==) return n;
int index = ;
for (int i=; i<n; ++i)
{
if (A[i]!=elem)
{
A[index++]=A[i];
}
}
return index;
}
};

Tips:

设定一个指针,始终指向要插入的元素的为止。

或者更简洁一些,用STL函数直接一行代码搞定:

class Solution {
public:
int removeElement(int A[], int n, int elem) {
std::distance(A,remove(A,A+n,elem));
}
};

==================================

第二次过这道题,试了几次才AC,代码如下:

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if ( nums.size()== ) return ;
int last = nums.size()-;
while ( nums[last]==val && last> ) last--;
for ( int i=; i<=last; ++i )
{
if ( nums[i]==val )
{
std::swap(nums[i], nums[last]);
last--;
while (nums[last]==val && last> ) last--;
}
}
return last+;
}
};

设立一个尾部的指针,保持尾部指针指向的元素不是val。

从前向后遍历,发现val就与last所指代的元素交换,最后返回last+1即可。

这么做虽然可以AC,而且效率还可以,但是确实麻烦了。原因是受到解题思维的影响,反而忽视最直接的办法了。

【Remove Elements】cpp的更多相关文章

  1. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  2. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  3. 【Valid Number】cpp

    题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...

  4. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  5. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  6. 【Spiral Matrix】cpp

    题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...

  7. 【Combination Sum 】cpp

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  8. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  9. 【Next Permutation】cpp

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

随机推荐

  1. eCharts图表(polar极坐标图)

    极坐标图 HTML: <div id="eChart"></div> css: #eChart{ width:500px; height:500px; } ...

  2. Java开发工具IntelliJ IDEA本地历史记录的使用方法

    IntelliJ IDEA的本地历史记录可以帮助用户记录并跟踪本地项目的更改,防止项目的意外丢失或来源于IDE之外的项目更改.本教程将展示如何使用本地历史记录查看和恢复某些项目更改. 1 .从头开始创 ...

  3. 如何查看win10已激活密钥?查看win10已激活完整密钥的方法!

    如何查看win10已激活密钥?查看win10已激活完整密钥的方法! HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/So ...

  4. VMware下Centos6.4安装

    VMware(Virtual Machine ware)是一个“虚拟PC”软件公司,提供服务器.桌面虚拟化的解决方案. 小伙伴们网上下载VMware11,一路下一步自己安装吧!!! 打开 VMware ...

  5. Python核心编程——多线程threading和队列

    线程与进程类似不过他们是在同一个进程下执行的,并共享相同的上下文.可以将他们认为是在一个主进程或“主线程”中运行的迷你进程. 线程包括开始.执行顺序和结束三部分.它有一个指令指针,用于记录当前运行的上 ...

  6. Windows环境下在Oracle VM VirtualBOX下克隆虚拟机镜像(克隆和导入)

    Windows环境下在Oracle VM VirtualBOX下克隆虚拟机镜像: 注:直接复制一个.vdi 虚拟硬盘再挂上去就可以,但Virtualbox居然提示UUID重复,无法使用. 则,可以通过 ...

  7. 启动Jmeter时遇到的几种错误

    1.权限不够 解决办法:用管理员权限运行 2.sdk版本太低 解决办法:1)查看当前sdk版本:java -version 2)安装sdk1.7或以上版本(jmeter3.0版本要用sdk1.7及以上 ...

  8. Oracle grant connect, resource to user语句中的权限

    博主在 Oracle 11g r2上测试(测试日期:2017.10.30): 用sys登陆到oracle中,执行以下两条语句: select * from role_sys_privs WHERE R ...

  9. 通过jQuery遍历div里面的checkbox

    遍历: $('#queryUser2 input[type="checkbox"]:checked').each( function () { a = a + $(this).va ...

  10. linux下避免僵尸进程的几种方法

    linux下我们可以调用fork函数创建子进程,创建的子进程将会得到父进程的数据空间.堆.栈......副本(采用写时复制机制),子进程将会继承父进程的信号掩码.信号处理方式.当前工作目录.会话id. ...