[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 ...
随机推荐
- P4145 上帝造题的七分钟2
题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段数中每个数都开平方(下取整)的操作. 第三分钟,k说,要能查询,于是便有了求一段 ...
- java后台简单从阿里云上传下载文件并通知前端以附件的形式保存
一. 首先开通阿里的OSS 服务 创建一个存储空间在新建一个Bucket 在你新建的bucket有所需的id和key 获取外网访问地址或者是内网 看个人需求 我使用的是外网(内网没用过 估计是部署到阿 ...
- Google 里的软件工程学
简评:原文作者 Fergus Henderson 在 Google 工作了 10 年以上,目前负责 Google 的 text-tospeech 工程小组.有很多书籍或文章会从 商业/管理 等非技术角 ...
- 定期删除Azure存储账号下N天之前的数据文件-ARM
######RemoveStorageBlob*DaysOld-ARM##### <# .SYNOPSIS Remove all blob contents from one storage a ...
- psp0级报告
计划 1.1需求描述: 现在市场上有很多的面向小学生的题卡,但是这习题卡不但价格昂贵,而且每次做题的内容基本都是固定.针对这些问题,开发出了这款网页在线答题系统,每次的题目都有所不同,可以跟快更好提高 ...
- leetcode-39-组合总和(有趣的递归)
题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...
- LeetCode 795. Number of Subarrays with Bounded Maximum
问题链接 LeetCode 795 题目解析 给定一个数组A,左右范围L.R.求子数组的数量,要求:子数组最大值在L.R之间. 解题思路 子数组必须连续,利用最大值R对数组进行分段,设定变量 left ...
- Python3 操作系统与路径 模块(os / os.path / pathlib)
#!/usr/bin/env python # coding=utf-8 __author__ = 'Luzhuo' __date__ = '2017/5/7' import os def os_de ...
- flask _bootstrap中使用flash
在模板中获取flash闪现的那段代码要和内容块放在同一级别上.不然网页上是看不到闪现的内容的. 比如在基模板里定义一个content block ,里面一个是get_flashed_messages代 ...
- Mac下安装eclipse(Mac 10.12/JDK/tomcat)
1.到官网https://www.eclipse.org/downloads/eclipse-packages/下载安装包 2.安装 注意:安装ecllipse时一定要安装JDK先,最新版本的ecli ...