【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 ...
随机推荐
- cocos2d-x 配置教程
下载cocos2d-x并解压(解压的路径不要带空格,不然以后用cygwin编译工程的时候会出现错误),我的解压到了D:\cocos2d-2.1rc0-x-2.1.2(我的版本是cocos2d-2.1r ...
- F5-WAF-12.0
平台: CentOS 类型: 虚拟机镜像 软件包: f5bigip basic software security waf 服务优惠价: 按服务商许可协议 云服务器费用:查看费用 立即部署 产品详情 ...
- 腾讯云服务器手动和自动安装WordPress网站程序
如果我们需要建站的话,对于基础个人网站.博客建站选择基础的1Mbps带宽配置的1GB内存的腾讯云服务器还是够用的,且如果我们需要用来建网站的话可以手工添加程序,以及有些面板,比如宝塔面板是自带CMS程 ...
- java核心技术 要点笔记1
第1章 1.java特性 简单性,java语法是一个C++语法的纯净版本. 面向对象,java将重点放在数据和对象的接口上.java与C++的主要不同点在于多继承,在java中实现多继承的机制是采用接 ...
- Mac Office 2016 卸载
https://support.office.com/zh-cn/article/%E5%8D%B8%E8%BD%BD-Office-2016-for-Mac-eefa1199-5b58-43af-8 ...
- AngularJs学习笔记-组件生命周期
组件生命周期 (1)组件生命周期钩子 constructor:组件创建时被创建 ngOnChanges: 父组件修改或初始化子组件的输入属性时被调用,如果子组件没有输入属性,则永远不会被调用,它的首次 ...
- 题解 CF656G 【You're a Professional】
又是一道假黑题 它教会我们不要看难度标签 虽然这道题的数据范围很小,用cin能过,但我还是要讲一下快读 快读嘛,顾名思义,就是 快速读入 的意思 有同学就会问了,快速读入的原理是什么? 答:它的原理其 ...
- linux的一些指令
linux的一些指令 █查看指令參數man 指令,如:man ls ,按q鍵退出 █查看文件列表ls -lht 按時間排序ll 列表ls 列表 目錄cd 目錄名稱 進入路徑cd .. 返回上層路徑 █ ...
- css中如何把鼠标变成手
css中鼠标放上去变成手型怎么设置:其实就是一个属性的问题, css的cursor属性 cursor:pointer; 其实这个属性我也记了很多,到现在都容易拼写错误,不过好在编辑器有提示. defa ...
- 你所不知道的js的小知识点(1)
1.js调试工具 debugger <div class="container"> <h3>debugger语句会产生一个断点,用于调试程序,并没有实际功能 ...