当前位置:网站首页>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 :
边栏推荐
- What are the types of system tests? Let me introduce them to you
- Little knowledge about TXE and TC flag bits
- Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
- Kwai applet guaranteed payment PHP source code packaging
- 力争做到国内赛事应办尽办,国家体育总局明确安全有序恢复线下体育赛事
- The way fish and shrimp go
- [knowledge map paper] Devine: a generative anti imitation learning framework for knowledge map reasoning
- 静态路由配置全面详解,静态路由快速入门指南
- The generosity of a pot fish
- BizDevOps与DevOps的关系
猜你喜欢
Nanny level tutorial: Azkaban executes jar package (with test samples and results)
Nacos microservice gateway component +swagger2 interface generation
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
科普 | 什么是灵魂绑定代币SBT?有何价值?
常见的磁盘格式以及它们之间的区别
牛熊周期与加密的未来如何演变?看看红杉资本怎么说
神经网络与深度学习-5- 感知机-PyTorch
JVM memory and garbage collection-3-runtime data area / method area
Usage of hydraulic rotary joint
随机推荐
MySQL查询为什么没走索引?这篇文章带你全面解析
Random walk reasoning and learning in large-scale knowledge base
文盘Rust -- 给程序加个日志
Cross modal semantic association alignment retrieval - image text matching
burpsuite
leetcode 869. Reordered Power of 2 | 869. 重新排序得到 2 的幂(状态压缩)
nmap工具介绍及常用命令
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
Direct addition is more appropriate
静态路由配置全面详解,静态路由快速入门指南
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!!!
List of top ten domestic industrial 3D visual guidance enterprises in 2022
2022国内十大工业级三维视觉引导企业一览
#797div3 A---C
阿南的判断
Master go game through deep neural network and tree search
I don't know. The real interest rate of Huabai installment is so high
Ml self realization / logistic regression / binary classification
Talk about the cloud deployment of local projects created by SAP IRPA studio
The generosity of a pot fish