题目:

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. Miner3D Enterprise 企业版

    ——极致的视觉数据挖掘和知识发现解 Miner3D Enterprise 企业版是一个有力的综合数据驱动的三维可视化和数据分析解决方案.金融师.药剂师.生物技术或化学材质科研人员.地质学家.石油开采专 ...

  2. Android 浮动按钮的伸缩效果

    在做项目时想增加点动感,于是就有如下效果: 实现起来也很简单,通过属性动画和recyclerview 滑动结合就很好实现了. 通过给recycleview添加一个滑动监听:通过滚动的差值来处理动画 m ...

  3. python decorator 用法

    https://www.zhihu.com/question/31265857 http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html ...

  4. python+selenium之自动生成excle,保存到指定的目录下

    进行之自动化测试,想把自动生成的excle保存到指定的目录下.网上百度的代码如下: import xlwt import time time = time.strftime ('%Y%m%d%H%M% ...

  5. LeetCode Nim Game (简单nim)

    题意: 有一堆石子,里面有n个石头,每次可以从中取出1~3个,两人轮流取,最后一个石子被谁取走即为赢家.你先取,问最后谁赢? 思路: n%4>0则先手赢,因为每次总是可以给对方留4个石子的倍数, ...

  6. c++ STL list容器成员函数

    list是一个双链表. 函数 描述 void l.assign (int n, const val) void l.assign (it first, it last) 将链表l初始化为n个相同的va ...

  7. Codeforces 763A. Timofey and a tree

    A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...

  8. ffmpeg 资源[转]

    http://blog.csdn.net/leixiaohua1020/article/details/15811977 一. FFmpeg主站 1. FFmpeg的源码发布,资料 网址:   htt ...

  9. innobackupex基于binlog日志的恢复 -- 使用mysqlbinlog恢复

    备份先做一次完整备份: innobackupex --defaults-file=/etc/my.cnf --user root --password chengce243 /data/mysqlba ...

  10. 使用FolderBrowserDialog组件选择文件夹

    实现效果: 知识运用: FolderBrowserDialog组件的ShowDialog方法 //弹出选择路径对话框 public DialogResult ShowDialog() 和Selecte ...