简体   繁体   English

在无序数组中,删除元素O(n)的时间复杂度吗?

[英]In an unordered array, isnt the time complexity of removing an element O(n)?

I read somewhere that it is O(1). 我在某处读到它是O(1)。 However, if we need to remove an element from an array we need to swap all the elements to the left and in the worst case wont will it need to make N swaps which makes the complexity O(n)? 但是,如果需要从数组中删除一个元素,则需要将所有元素交换到左侧,在最坏的情况下,是否需要进行N次交换,从而使复杂度为O(n)? I am confused?! 我很困惑?! Also is there a difference between an unordered and an unsorted array?? 在无序数组和未排序数组之间也有区别吗?

由于数组是无序的,因此您可以用最后一个元素填充孔,然后将数组缩短1。无需将每个元素向下移动一个位置。

根据要删除元素的方式(即通过提供元素的索引或值),它是O(1)O(n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 static 数组中删除最后一个元素的时间复杂度 - O(n) 还是 O(1)? - Time Complexity of removing the last element from a static array- O(n) or O(1)? 在 O(n) 时间复杂度中删除两个列表中的公共元素 - Removing Common elements in two List in O(n) Time Complexity 以o(n)或o(1)的时间复杂度对整数数组进行排序 - Sorting an array of integers in time complexity of o(n) or o(1) 优化程序以查找数组中第k个最小元素(Java,预期时间复杂度O(n)) - Optimization of Program to find kth smallest element in an array (Java, Expected time complexity O(n)) 我们可以找到线性时间复杂度 O(n) 的数组中每个元素的等级吗? - Can we find rank of each element in array with Linear Time Complexity O(n)? 查找特定的缺失数组元素:O(n + k)复杂度 - Finding a Particular Missing Array Element: O(n+k) Complexity 最小跳转数组递归时间复杂度应为 O(n^n) 或 O(n!) - Minimum Jump Array Recursive Time Complexity should be O(n^n) or O(n!) 在 O(log n) 时间复杂度中查找排序数组的总和 - Finding sum of a sorted array in O(log n) Time Complexity 数组子集问题[时间为O(n)的最小空间] - Array Subset problem [least space with O(n) time complexity] 在数组中查找不同值的O(1)空间复杂度的O(N ^ 2)时间复杂度 - Alternative O(N^2) time complexity with O(1) space complexity for finding distinct values in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM