当前位置:网站首页>Leetcode daily practice - 27. Remove elements
Leetcode daily practice - 27. Remove elements
2022-07-26 19:49:00 【An airliner flying to the stars】
Preface
Wassup guys! I am a Edison
It's today LeetCode Upper leetcode 27. Remove elements
Let’s get it!

List of articles
1. Topic analysis
Array
numsAnd a valueval, You need In situ Remove all values equal tovalThe 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 ) O(1) 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 .
Example 1:
Example 2:
2. Title diagram
Train of thought
Open up a and nums A new array of the same size , It's not val Copy the value of to the new array
First of all, the pointer src Point to nums The starting position in the array ; The pointer dst Point to newNums The starting position in the array 
here ,src The element pointed to is 0, No 2, So put the numbers 0 Put it in newNums Array , The pointer src and dst Move backward at the same time , As shown in the figure 
here ,src The element pointed to is 1, No 2, So put the numbers 1 Put it in newNums Array , The pointer src and dst Move backward at the same time , As shown in the figure 
here ,src The element pointed to is 2, And val The values are equal , Then put the pointer src Move back , The pointer dst Immobility , As shown in the figure 
here ,src The element pointed to is 2, And val The values are equal , Then put the pointer src Move back , The pointer dst Immobility , As shown in the figure 
here ,src The element pointed to is 3, No 2, So put the numbers 3 Put it in newNums Array , The pointer src and dst Move backward at the same time , As shown in the figure 
here ,src The element pointed to is 0, No 2, So put the numbers 0 Put it in newNums Array , The pointer src and dst Move backward at the same time , As shown in the figure 
here ,src The element pointed to is 4, No 2, So put the numbers 4 Put it in newNums Array , The pointer src and dst Move backward at the same time , As shown in the figure 
here ,src The last element has been pointed to 2, And val The values are equal , So end the cycle , As shown in the figure
The time complexity of this method is O ( n ) O(n) O(n), The complexity of space is also O ( n ) O(n) O(n); It doesn't fit the question
Train of thought two
This method is an upgrade of idea one , Do not open up new arrays , Use double pointers directly in the original array
The pointer src and dst At the same time point to nums Starting position , As shown in the figure 
here src The element pointed to is 0, It's not equal to val(2), Then use src Point to the element Cover fall dst Pointing elements , then src and dst Move backward at the same time , As shown in the figure 
here src The element pointed to is 1, It's not equal to val(2), Then use src Point to the element Cover fall dst Pointing elements , then src and dst Move backward at the same time , As shown in the figure 
here src The element pointed to is 2, be equal to val(2), Then just put the pointer src Move back , The pointer dst Immobility , As shown in the figure 
here src The element pointed to is still 2, be equal to val(2), Still only put the pointer src Move back , The pointer dst Immobility , As shown in the figure 
here src The element pointed to is 3, It's not equal to val(2), Then use src Point to the element Cover fall dst Pointing elements , then src and dst Move backward at the same time , As shown in the figure 
here src The element pointed to is 0, It's not equal to val(2), Then use src Point to the element Cover fall dst Pointing elements , then src and dst Move backward at the same time , As shown in the figure 
here src The element pointed to is 4, It's not equal to val(2), Then use src Point to the element Cover fall dst Pointing elements , then src and dst Move backward at the same time , As shown in the figure 
here src Point to last element 2, be equal to val(2), So it's just over , return dst The length of 
therefore dst Is used to delete the array equal to val Value , Then put not equal to val Values of are put back in turn ! So the structure of this problem is as follows 
3. Code implementation
Interface code
int removeElement(int* nums, int numsSize, int val){
int src = 0;
int dst = 0;
int i = 0;
while (src < numsSize) {
if (nums[src] != val) {
nums[dst] = nums[src];
src++;
dst++;
}
else {
src++;
}
}
return dst;
}
Submit results 
边栏推荐
- PyQt5快速开发与实战 3.6 打包资源文件
- Svn - detailed documentation
- 学习Muduo中ChatRoom实现的各种细节和思考
- IM即时通讯开发如何压缩移动网络下APP的流量消耗
- 手机app测试用例怎么写?手机app测试点有哪些?
- Win11 U盘驱动异常怎么调整为正常?
- [PHP] common header definitions
- 关于接口测试你想知道的都在这儿了
- Do you know the difference between safety test, functional test and penetration test?
- Spatiotemporal prediction 4-graph WaveNet
猜你喜欢

Redis introduction

Implementing DDD based on ABP -- domain logic and application logic

Talk about how to use redis to realize distributed locks?

J1: why is redis so fast + basic structure

中天钢铁在 GPS、 AIS 调度中使用 TDengine

PyQt5快速开发与实战 3.6 打包资源文件
![[yolov5] - detailed version of training your own dataset, nanny level learning, logging, hand-in-hand tutorial](/img/34/5ab529ff6d8d0fd3827c440299964d.png)
[yolov5] - detailed version of training your own dataset, nanny level learning, logging, hand-in-hand tutorial

拿铁DHT-PHEV产品响当当,销量会不会让李瑞峰想通了呢?

cuda11.2对应pytorch安装

Solidity中call函数详解
随机推荐
Using MySQL master-slave replication delay to save erroneously deleted data
Openstack virtual machine network card is renamed cirename0
C # upper computer development - modify the window icon and exe file Icon
Principle analysis and source code interpretation of service discovery
What is federated graph machine learning? A summary of the latest "federal map machine learning: concepts, techniques, and Applications" at the University of Virginia
Svn - detailed documentation
[PHP] use file_ get_ Contents() sends get and post requests
Three paradigms of database design
Test interview question set UI automated test
关于接口测试你想知道的都在这儿了
Difficult performance problems solved in those years -- ext4 defragmentation
eadiness probe failed: calico/node is not ready: BIRD is not ready: Error querying BIRD: unable to c
Conda+pytorch environment tutorial
DOM案例:10秒倒计时-写跳转页面相关的知识
工作13年后,个人的一点软件测试经历及感想……
canvas 图形
Familiarize you with the "phone book" of cloud network: DNS
PHP 替换中文字符的方法
Volatile keyword of JVM memory model
带你熟悉云网络的“电话簿”:DNS