当前位置:网站首页>Leetcode question brushing record | 27_ Removing Elements
Leetcode question brushing record | 27_ Removing Elements
2022-07-08 02:10:00 【coder_ sure】
leetcode Record of writing questions |27 _ Remove elements
author github link : github link
Force to buckle 27 topic
type : Array question
subject :
Give you an array nums
And a value val
, You need In situ
Remove all values equal to val
The elements of , And return the new length of the array after removal .
Don't use extra array space , You have to use only O(1)
Additional space and In situ
Modify input array .
The order of elements can be changed . You don't need to think about the elements in the array that follow the new length .
explain
Why is the return value an integer , But the output answer is array ?
Please note that , The input array is based on 「 quote 」 By way of transmission , This means that modifying the input array in a function is visible to the caller .
You can imagine the internal operation as follows :
// nums In order to “ quote ” By way of transmission . in other words , Do not make any copies of the arguments
int len = removeElement(nums, val);
// Modifying the input array in a function is visible to the caller .
// Based on the length returned by your function , It prints out the array Within this length All elements of .
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Example 1
Input :nums = [3,2,2,3], val = 3
Output :2, nums = [2,2]
explain : Function should return the new length 2, also nums The first two elements in are 2. You don't need to think about the elements in the array that follow the new length . for example , The new length returned by the function is 2 , and nums = [2,2,3,3] or nums = [2,2,0,0], It will also be seen as the right answer .
Example 2
Input :nums = [0,1,2,2,3,0,4,2], val = 2
Output :5, nums = [0,1,4,0,3]
explain : Function should return the new length 5, also nums The first five elements in are 0, 1, 3, 0, 4. Note that these five elements can be in any order . You don't need to think about the elements in the array that follow the new length .
Their thinking
Train of thought reminder : Define a pointer before and after ( Front and rear double pointers )
Train of thought details :
- Define two pointers , for example
left
andright
- take
left
Put at the beginning ,right
Put at the end of the array , That is to say, make :left = 0
right = nums The length of
left
To the right ,right
Move to the left .- When
left
Pointer toIt's not equal to val
Of the elements ,left
Traverse right in turn - When
left
Pointer tobe equal to val
Of the elements , takeleft
Replace the element pointed to with the presentright-1 Pointing elements
. - After exchange ,
right-1
- until left ≥ \geq ≥right Back when
left
( Return to remove all val The value of the value )
python
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
# Define a pointer before and after ( Front and rear double pointers )
left = 0
right = len(nums)
while(left<right):
if nums[left] == val:
nums[left] = nums[right-1]
right = right -1
else:
left = left+1
return left
c++
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
// Double pointer One at the beginning One at the end
int right = nums.size();
int left = 0;
while (left<right){
if(nums[left] == val){
nums[left] = nums[right -1];
right--;
}else{
left++;
}
}
return left;
}
};
A small detail :
- When
left
Pointer tobe equal to val
Of the elements , takeleft
Replace the element pointed to with the presentright-1 Pointing elements
.
You can also exchange the elements pointed to by two pointers , But the running time will be left
Replace the element pointed to with the present right-1 Pointing elements
To get longer .
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int left = 0;
int right = nums.size();
while(left<right){
if(nums[left]==val){
swap(nums[left], nums[right-1]);// Swap the elements pointed to by two pointers
right--;
}
else left++;
}
return left;
}
};
Method 1 And the following methods 2 Comparison of running time :
边栏推荐
- Get familiar with XML parsing quickly
- Cross modal semantic association alignment retrieval - image text matching
- JVM memory and garbage collection-3-direct memory
- Ml self realization / logistic regression / binary classification
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- The function of carbon brush slip ring in generator
- 很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
- 给刚入门或者准备转行网络工程师的朋友一些建议
- [recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
- Keras' deep learning practice -- gender classification based on inception V3
猜你喜欢
[knowledge map] interpretable recommendation based on knowledge map through deep reinforcement learning
Ml self realization /knn/ classification / weightlessness
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
云原生应用开发之 gRPC 入门
XMeter Newsletter 2022-06|企业版 v3.2.3 发布,错误日志与测试报告图表优化
Ml self realization / logistic regression / binary classification
微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
系统测试的类型有哪些,我给你介绍
Remote sensing contribution experience sharing
随机推荐
保姆级教程:Azkaban执行jar包(带测试样例及结果)
Nanny level tutorial: Azkaban executes jar package (with test samples and results)
力扣5_876. 链表的中间结点
mysql/mariadb怎样生成core文件
魚和蝦走的路
JVM memory and garbage collection-3-direct memory
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
进程和线程的退出
Introduction à l'outil nmap et aux commandes communes
CV2 read video - and save image or video
Master go game through deep neural network and tree search
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
node js 保持长连接
cv2读取视频-并保存图像或视频
2022国内十大工业级三维视觉引导企业一览
leetcode 866. Prime Palindrome | 866. prime palindromes
Installing and using mpi4py
What are the types of system tests? Let me introduce them to you
Mouse event - event object
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing