题目

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. shell脚本调试技巧

    shell脚本调试之工具——bashdb http://www.cnblogs.com/itcomputer/p/5011845.html

  2. linux下安装和卸载mysql

      卸载: 1 . rpm -qa | grep -i mysql命令查看已经安装过的组件.   2. 使用yum -y remove命令卸载已经安装的MySQL组件,使用下面的命令,对于上面已经安装 ...

  3. SpringMVC-响应数据和结果视图

    返回值分类 1. 字符串 controller 方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址. 2. void 在 controller 方法形参上可以定义 request 和 ...

  4. Github的基本功能

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Fadeoc Khaos链接:http://www.zhihu.com/question/20070065/answer/30 ...

  5. nginx入门学习步骤(linux)

    一.nginx下载(nginx-1.9.9) http://nginx.org/download/ 二.解压到指定文件夹 tar -zxvf 解压缩文件 三.设置配置信息 在nignx解压文件夹内执行 ...

  6. Mybatis自查询递归查找子

    先看一下数据库 主键id,名称product_code,父parent,和kind 设计菜单类 setter,getter Dao public interface ProductMapper { L ...

  7. 2017年9月22日作业 c++算术运算符 自增 自减 逻辑运算符 位运算符 条件运算符(三元运算符)

    作业1: c++算术运算符试题,分析下面程序的输出结果是什么 //第一个: int x=8999;int value=x*1000/1000; //第二个 int x=8999;int value=x ...

  8. vue切换路由时动画

    安装个包 npm i nprogress 直接导入使用 最终的效果就是

  9. HH的项链题解(离线思想+链表+树状数组)

    本人第一篇博客重磅推出!!! 希望各位朋友以后多多捧场也多给写意见(我个人喜欢把题解写得啰嗦一点,因为这样方便理解,各位巨佬勿喷) 来讲一道提高+/省选-的骚题:HH的项链(这个HH你理解成皇后呵呵哈 ...

  10. ZendFramework-2.4 源代码 - 关于MVC - Model层

    所谓的谓词Predicate // ------ 所谓的谓词 ------ // 条件 case.3 $where = new \Zend\Db\Sql\Where(); $expression = ...