[LeetCode] Remove Element题解
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3Your function should return length = 2, with the first two elements of nums being 2.
这道题如果没有其他限制,另开一条数组,将不等于val的数存进去就ok了,但是它限制了我们另开一条数组,所以可以用c++ STL中vector的erase函数直接在原数组中更改:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
while(remove(nums,val)){
}
return nums.size();
}
bool remove(vector<int>& nums, int val){
vector<int>::iterator iter;
for(iter=nums.begin();iter!=nums.end();iter++){
if(*iter==val){
nums.erase(iter);
return true;
}
}
return false;
}
};
而在一些语言中没有erase函数可以用,这么办?不用erase函数的话,我们可以这样处理:
int removeElement(vector<int>& nums, int val) {
int cnt = 0;
for(int i = 0 ; i < nums.size() ; ++i) {
if(nums[i] == val)
cnt++;
else
nums[i-cnt] = nums[i];
}
return nums.size()-cnt;
}
或者
int removeElement(int A[], int n, int elem) {
int begin=0;
for(int i=0;i<n;i++){
if(A[i]!=elem){
A[begin++]=A[i];
}
}
return begin;
}
[LeetCode] Remove Element题解的更多相关文章
- [LeetCode] Remove Element 分析
Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...
- LeetCode Remove Element
原题链接在这里:https://leetcode.com/problems/remove-element/ 题目: Given an array and a value, remove all ins ...
- [LeetCode] Remove Element 移除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- [LeetCode] Remove Element (三种解法)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- LeetCode——Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- [Leetcode] remove element 删除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- leetcode Remove Element python
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] ...
- LeetCode Remove Element删除元素
class Solution { public: int removeElement(int A[], int n, int elem) { ]; int i,num=n; ;i<n;i++){ ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
随机推荐
- javascript获取网址参数
通过以上图片,就可以很好的理解: location.href location.protocol location.host location.hostname location.port locat ...
- [Objective-C语言教程]错误处理(22)
在Objective-C编程中,错误处理由Foundation框架中提供的NSError类提供处理. 与仅使用错误代码或错误字符串相比,NSError对象封装了更丰富且更具可扩展性的错误信息. NSE ...
- leetcode-74-搜索二维矩阵
题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: ...
- 在Eclipse之中调试FastDFS-storage
FDFS版本为5.03 1.首先在eclipse之中创建一个C/C++工程,取名为FastDFS_v5.03 2.将FastDFS源码解压后拷贝到新创建的工程目录下,然后在ecipse之中刷新下工程就 ...
- GeneXus学习笔记——创建一个知识库 哈哈哈哈!
终于想到写什么东西了(绝对不是因为懒 好吧 信我) 那么进入正题 我们写项目的时候 遇到一些问题啥的 总会听到大佬说:“这有什么难的 说到底不就是简单的增删改查么" 然后我们就会露出 Σ ...
- PS2模拟器 PCSX2 新手向
1.模拟器的下载 1.1百度网盘地址:http://pan.baidu.com/s/1i3kt7bJ (已经整合了PS2BIOS的模拟器下载,比较新的版本,适合新手) 1.2高端玩家可以下载: 官网g ...
- windows下python管理右键菜单
实现很简单,不记得什么时候写的了,贴出来希望能有所价值 """ Windows中创建右键菜单 """ import os import sy ...
- c# 操作excle[转]
//引用Microsoft.Office.Interop.Excel.dll文件 //添加using using Microsoft.Office.Interop.Excel; using Excel ...
- 在国内运行Flutter配置的正确姿势--如出现Oops; flutter has exited unexpectedly.
如果根据flutter的官网教程,在运行flutter doctor时无法下载依赖,请根据以下步骤 export PUB_HOSTED_URL=https://pub.flutter-io.cn ex ...
- Mac 10.12安装PDF浏览工具Foxit Reader
说明:永久没费的跨平台PDF浏览工具. 下载: (链接: https://pan.baidu.com/s/1pLEAoXH密码: is5j)