【Remove Elements】cpp
题目:
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的更多相关文章
- leetcode 【 Remove Element 】python 实现
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Valid Number】cpp
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Spiral Matrix】cpp
题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spira ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Next Permutation】cpp
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
随机推荐
- CocoStudio UIButton setPressedActionEnabled(true) 子控件不跟着缩放
具体情况是这样的:美术给了我 一个按钮的背景图片 一个按钮的文字图片,用背景图片创建一个button,然后把文字图片添加进去(注意关闭文字图片的交互功能) 设置UIButton setPressed ...
- WORD窗体保护密码清除
Word 2003破解方法如下:1.用Word打开已设置有密码的“保护文档”(原始DOC文件):2.菜单中选择“文件”→“另存为Web页”,保存为HTML文件然后关闭Word:3.用文本编辑器(如:记 ...
- TFS看板的设计
列 产品开发的整个流程如下图,将流程配置到看板的列: 需求池-->就绪-->开发-->测试-->待验收 -->待发布 -->已关闭 一般将Bug和需求放在一块看版上 ...
- Linux命令技巧:如何在Linux下重命名多个文件
我知道我可以用mv命令重命名文件.但是当我想重命名很多文件怎么办?如果为每个文件都这么做将会是很乏味的.有没有办法一次性重命名多个文件? 在Linux中,当你想要改变一个文件名,使用mv命令就好了.然 ...
- 爬虫爬取代理IP池及代理IP的验证
最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...
- Luogu [P1958] 上学路线_NOI导刊2009普及(6)
上学路线_NOI导刊2009普及(6) 题目详见:上学路线_NOI导刊2009普及(6) 这是一道基础的DFS(深搜)题,堪称模板,是新手练习搜索与回溯的好题选. 大致思路:从(1,1)开始搜索,每次 ...
- 通过Jquery获取RadioButtonList选中值
推荐 使用第二种,第一种有时候不起作用 第一种:通过find方法 获取RadioButtonList所选中的值 <script type="text/javascript"& ...
- AngularJS 数组
AngularJS数组就像Javascript数组 <!DOCTYPE html><html><head><meta http-equiv="Con ...
- Bootstrap历练实例:警告框(Alert)插件的方法
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 数据结构期末复习( はち)--VOA图关键路径求法
题目如下图: 注:将123456当成abcdef. 事件最早发生事件求法:找从原点到该事件的最长路径(从前往后推) 对a:Ve=0 对b:Ve=max{ 2 , 15+4 }=19 对c:Ve=15 ...