[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 ...
随机推荐
- 爬虫3:requests库
一个简单易用的http库,多用于第一步,爬取网站源码 简单例子 import requests response = requests.get('https://www.baidu.com ...
- python --爬虫--爬取百度翻译
import requestsimport json class baidufanyi: def __init__(self, trans_str): self.lang_detect_url = ' ...
- delphi 10.2 ----简单的叠乘例子
unit Unit11; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...
- python中type、class、object的区别
type 一. type可以用来返回一个对象的类型 例如: 二. 由于Python中一切皆对象,也就是说Python中的任何变量类型都是可以被修改的,这也是Python等动态编程语言的特点.type的 ...
- linux上搭建nginx+php+mysql环境详细讲解
1.mysql安装 #安装编译环境 yum install -y gcc gcc-c++ gcc-devel g++ g++-devel; yum install -y wget yum instal ...
- 2019年北航OO第二次博客总结
一.多线程电梯系列作业设计策略 1. 第一次作业——"FAFS傻瓜电梯" 第一次作业是先来先服务的"傻瓜电梯",我当时觉得这个设计未免太简单了,于是就在傻瓜电梯 ...
- 直接线性变换解法(DLT)用于标定相机
直接线性变换法是建立像点坐标和相应物点物方空间坐标之间直接的线性关系的算法.特点:不需要内外方位元素:适合于非量测相机:满足中.低精度的测量任务:可以标定单个相机. 1 各坐标系之间的关系推导直接线性 ...
- initializer_list
initializer_list是一种模板类型,定义initializer_list对象是,必须说明列表中所含元素的类型: initializer_list<Type> lst{a, b, ...
- Ubuntu 16.04防火墙
防火墙(ufw) 说明:简单版本的防火墙,底层依赖于iptables. 安装:sudo apt-get install ufw 查看状态:sudo ufw status 开启/关闭:sudo ufw ...
- 南昌 Max answer
https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a interva ...