当前位置:网站首页>数组元素移除问题
数组元素移除问题
2022-07-24 10:28:00 【认真编程的紫衫龙王】
一.数组理论基础
- 移除数组元素的底层逻辑:移位、覆盖
- 函数实现:del、pop()、remove()等
二.暴力算法(两重循环)
0. 参考思路原题:来自Leetcode题库27题
- 算法思路(可参考我在Leetcode上写的题解)
新手向——python暴力求解移除元素问题
https://leetcode.cn/problems/remove-element/solution/by-zi-shan-long-wang-nr8n/ - 详细代码
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ n=len(nums) # count记录val的个数 # i为用于指示位置的循环变量 count=0 i=0 #暴力求解,二重循环 #第一重循环遍历数组元素 while (i<n-count): # 判断是否等于val,相等进入第二重循环;不相等变量加1继续执行第一重循环 if nums[i]==val: # 第二重循环遍历相等元素后的所有元素并且整体前移一位 for j in range(i+1,n): nums[j-1]=nums[j] count+=1 else: i+=1 # 返回数组长度与重复次数之差 return n-count - 时空复杂度:时间复杂度:O(n^2) 空间复杂度:O(1)
三.双指针算法
0. 参考思路原题:来自Leetcode题库26题
- 算法思路:一个for循环+快慢两个指针来代替暴力解法中的双for循环,其中快指针用于寻找新数组的元素 (新数组就是不含有重复元素的数组),需跟随for循环遍历原数组中全部元素;慢指针用于指向更新新数组下标的位置(更新条件为快指针所指元素是否在慢指针前的切片数组中),作为返回值输出
- 代码实现:
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# 初始化快慢指针
fast=slow=0
# 进入循环
while fast<len(nums):
# 判断元素是否有重复
if nums[fast] not in nums[:slow]:
# 对符合要求的元素更新
nums[slow]=nums[fast]
slow+=1
fast+=1
return slow四.相关题目推荐
边栏推荐
- 563页(30万字)智慧化工园区(一期)总体设计方案
- Websocket 协议解读-RFC6455
- Volcanic engine: open ByteDance, the same AI infrastructure, a system to solve multiple training tasks
- 谷歌联合高校研发通用模型ProteoGAN,可设计生成具有新功能的蛋白质
- Constant pointer, pointer constant
- Activity exception lifecycle
- Review of new services and functions of Amazon cloud technology in June 2022
- How does ribbon get the default zoneawareloadbalancer?
- Intranet remote control tool under Windows
- Web Security Foundation - file upload (file upload bypass)
猜你喜欢

很佩服的一个Google大佬,离职了。。

关联规则--2022年7月10日

Deployment and analysis of coredns

Differential restraint system -- 1 and 2 -- May 27, 2022

Will not be rejected! Learn the distributed architecture notes sorted out by Alibaba Daniel in 35 days, with a salary increase of 20K

What did zoneawareloadbalancer of ribbon and its parent class do?

String__

NIO知识点

MySQL——锁

Image processing: rgb565 to rgb888
随机推荐
Sub query of multi table query_ Single row and single column
Sentinel three flow control effects
Sentinel 三种流控模式
js函数调用下载文件链接
Record AP and map calculation examples
Implementation and traversal of binary tree and binary tree sorting tree
What is NFT? How to develop NFT system?
The best time to buy and sell stocks includes handling charges (leetcode-714)
Erlang learning 02
What does resource pooling and resource pooling mean?
The role of glpushmatrix and glpopmatrix
Home raiding III (leetcode-337)
Create a vertical seekbar from scratch
Set scrolling for the box
[sword finger offer II 115. reconstruction sequence]
Gaode map
How to solve the problem of robot positioning and navigation in large indoor scenes with low-cost solutions?
NIO知识点
Rust tokio:: task:: localset running mode
CMS vulnerability recurrence - foreground arbitrary user password modification vulnerability
https://leetcode.cn/problems/remove-element/solution/by-zi-shan-long-wang-nr8n/