当前位置:网站首页>Array element removal problem
Array element removal problem
2022-07-24 10:33:00 【Carefully programmed purple shirt Dragon King】
One . The basis of array theory
- Remove the underlying logic of array elements : displacement 、 Cover
- Function implementation :del、pop()、remove() etc.
Two . The brute force algorithm ( Double cycle )
0. Refer to the original topic : come from Leetcode Question bank 27 topic
- Algorithm ideas ( Please refer to me in Leetcode Explanation of the question written on )
Novice ——python Solve the problem of removing elements with violence
https://leetcode.cn/problems/remove-element/solution/by-zi-shan-long-wang-nr8n/ - Detailed code
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ n=len(nums) # count Record val The number of # i Is a cyclic variable used to indicate the position count=0 i=0 # Violent solution , Double cycle # The first loop traverses the array elements while (i<n-count): # Judge whether it is equal to val, Equality enters the second cycle ; Unequal variables plus 1 Continue with the first loop if nums[i]==val: # The second loop traverses all the elements after the equal elements and moves the whole forward one bit for j in range(i+1,n): nums[j-1]=nums[j] count+=1 else: i+=1 # Returns the difference between the length of the array and the number of repetitions return n-count - Complexity of time and space : Time complexity :O(n^2) Spatial complexity :O(1)
3、 ... and . Double pointer algorithm
0. Refer to the original topic : come from Leetcode Question bank 26 topic
26. Remove duplicate items from an ordered array
- Algorithm ideas : One for loop + Speed two pointers to replace the double in the violent solution for loop , The fast pointer is used to find the elements of the new array ( A new array is an array that does not contain duplicate elements ), Need to follow for Loop through all elements in the original array ; The slow pointer is used to point to the position where the subscript of the new array is updated ( The update condition is whether the element referred to by the fast pointer is in the slice array in front of the slow pointer ), Output as return value
- Code implementation :
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Initialize speed pointer
fast=slow=0
# Into the loop
while fast<len(nums):
# Judge whether the elements are repeated
if nums[fast] not in nums[:slow]:
# Update the elements that meet the requirements
nums[slow]=nums[fast]
slow+=1
fast+=1
return slowFour . Related topic recommendation
边栏推荐
- The method modified by private can be accessed through reflection. What is the meaning of private?
- Arduino + AD9833 波形发生器
- Simply use golang SQLC to generate MySQL query code
- Constant pointer, pointer constant
- Erlang学习01
- Differential restraint system -- 1 and 2 -- May 27, 2022
- js函数调用下载文件链接
- PostgreSQL rounding
- zoj-Swordfish-2022-5-6
- zoj1137+作业1--2022年5月28日
猜你喜欢

How does ribbon get the default zoneawareloadbalancer?

Websocket 协议解读-RFC6455

Volcanic engine: open ByteDance, the same AI infrastructure, a system to solve multiple training tasks

In depth analysis of common cross end technology stacks of app

MySQL - 索引的隐藏和删除

CMS vulnerability recurrence - ultra vires vulnerability

MySQL - normal index

Sentinel implements the persistence of pull pattern rules

WEB安全基础 - - -文件上传(文件上传绕过)

zoj1137+作业1--2022年5月28日
随机推荐
MySQL performance optimization (IV): how to use indexes efficiently and correctly
NLP introduction + practice: Chapter 2: introduction to pytorch
Kotlin advanced
Web Security Foundation - file upload (file upload bypass)
给你的网站加一个爱发电角标
MySQL - 多列索引
Erlang learning 01
每日三题 7.22
分布式锁-Redission 原理分析
binlog、iptables防止nmap扫描、xtrabackup全量+增量备份以及redlog和binlog两者的关系
Sentinel 流量控制快速入门
How does ribbon get the default zoneawareloadbalancer?
PC博物馆(1) 1970年 Datapoint 2000
CMS vulnerability recurrence - foreground arbitrary user password modification vulnerability
2022: I feel like sleeping for the first time during the day
JS function call download file link
Image processing: floating point number to fixed point number
Sub query of multi table query_ Single row and single column
Will not be rejected! Learn the distributed architecture notes sorted out by Alibaba Daniel in 35 days, with a salary increase of 20K
Android uses JDBC to connect to a remote database
https://leetcode.cn/problems/remove-element/solution/by-zi-shan-long-wang-nr8n/