Hướng dẫn python deque insert time complexity - python và chèn thời gian phức tạp

from collections import deque
d = deque("dbkd")
d.insert(2,"h")

Độ phức tạp thời gian của

import collections
de = collections.deque([1,2,3])
6 là gì? Nếu Deque được thực hiện như một danh sách được liên kết thì nó phải là
import collections
de = collections.deque([1,2,3])
7? Tôi có đúng không?

hỏi ngày 27 tháng 6 năm 2020 lúc 5:45Jun 27, 2020 at 5:45

Hướng dẫn python deque insert time complexity - python và chèn thời gian phức tạp

Đã trả lời ngày 27 tháng 6 năm 2020 lúc 5:48Jun 27, 2020 at 5:48

Avanwieringenavanwieringenavanwieringen

2.3583 huy hiệu vàng20 Huy hiệu bạc28 Huy hiệu đồng3 gold badges20 silver badges28 bronze badges

4

Nhận cuốn sách này -> Vấn đề về Array: Đối với các cuộc phỏng vấn và lập trình cạnh tranh

Deque (Hàng đợi kết thúc kép) là một cấu trúc dữ liệu có thể được sử dụng để chèn hoặc xóa các phần tử dữ liệu ở cả hai kết thúc. Nó được hỗ trợ trực tiếp trong Python thông qua mô -đun bộ sưu tập.

  • "Bộ sưu tập", là một mô -đun Python xác định Deque.Collections", is a Python Module that defines Deque.
  • Để bắt đầu sử dụng Deque trong chương trình Python của bạn, hãy sử dụng mã được đưa ra dưới đây.
import collections
de = collections.deque([1,2,3])
  • Deque yêu cầu thời gian O (1) để thực hiện các hoạt động của phụ lục và POP. Trong khi đó, một danh sách yêu cầu độ phức tạp O (n). Để tham khảo, máy mất 1S để thực hiện 10^6 hoạt động.
    For reference a machine takes 1s to perform 10^6 operations.
  • Các yếu tố có thể được truy cập tại bất kỳ chỉ mục. Giống như một mảng. Nếu trong một trường hợp ví dụ bạn cần truy cập (giả sử) Phần tử thứ 4, bạn có thể có thể truy cập chỉ bằng chỉ mục.

Hướng dẫn python deque insert time complexity - python và chèn thời gian phức tạp

Câu hỏi

Độ phức tạp về thời gian cho các hoạt động phụ lục và pop?

O(1)

O(n*logn)

O(n^2)

O(n)

Tham khảo phần giới thiệu ở trên.

Hoạt động trên Deque

Các hoạt động được hỗ trợ bởi Deque là:

  • nối
  • Phụ lục
  • Pop và Popleft
  • mục lục
  • chèn
  • gỡ bỏ
  • đếm
  • gia hạn
  • mở rộng
  • đảo ngược
  • quay
  • Len

1. append ():- Hàm này được sử dụng để chèn giá trị vào đối số của nó vào đầu bên phải của deque. Nối đơn giản là nối lại phần tử bạn nhập vào đầu bên phải của deque. :- This function is used to insert the value in its argument to the right end of deque. Append simply appends the element you enter to the right end of the deque.

import collections
de = collections.deque([1,2,3])
de.append(4)
de

2. Phụ lục ():- Hàm này được sử dụng để chèn giá trị vào đối số của nó vào đầu bên trái của deque. :- This function is used to insert the value in its argument to the left end of deque.

import collections
de = collections.deque([1,2,3])
de.append(4)
de.appendleft(0)
de

3. pop () & popleft ():- Các phương pháp này được sử dụng để xóa một đối số từ đầu bên phải và cũng từ đầu bên trái của deque tương ứng. :- These methods are used to delete an argument from the right end and also from the left end of the deque respectively.

import collections
de = collections.deque([1,2,3])
de.append(4)
de.appendleft(0)
de.pop()
de
de.popleft()
de

Câu hỏi

Độ phức tạp về thời gian cho các hoạt động phụ lục và pop?

Tham khảo phần giới thiệu ở trên.


Hoạt động trên Deque :- This function returns the first index of the value mentioned in arguments, starting searching from beg till end index. This simply searches the occurence for the 'ele'and returns it.

import collections
de = collections.deque([1,2,3])
de.append(4)
de.appendleft(0)
de.pop()
de.popleft()
de.index(2,0,2)

Các hoạt động được hỗ trợ bởi Deque là: :- This function inserts the value mentioned in arguments(a) at index(i) specified in arguments. This is similar to in array. It overrides the value, if the value is present in prior to the insertion at the given index.

import collections
de = collections.deque([1,2,3])
de.append(4)
de.appendleft(0)
de.pop()
de.popleft()
de.index(2,0,2)
de.insert(1,5)
de

nối

Phụ lục

Pop và Popleft

import collections
de = collections.deque([1,2,3])
de.append(4)
de.appendleft(0)
de.pop()
de.popleft()
de.index(2,0,2)
de.remove(2)
de.insert(-5,10)
de

mục lục :- This function removes the first occurrence of value mentioned in arguments. Searches, for the occurence of the element and deletes whenever, it finds the required element.

chèn

gỡ bỏ :- This function counts the number of occurrences of value mentioned in arguments. Increements to the value of the number of times a number is present.

import collections
de = collections.deque([1,2,3])
de.append(4)
de.appendleft(0)
de.pop()
de.popleft()
de.index(2,0,2)
de.insert(1,5)
de.remove(2)
de.append(1)
de.count(1)

Câu hỏi

Độ phức tạp về thời gian cho các hoạt động phụ lục và pop?

Tham khảo phần giới thiệu ở trên.

Hoạt động trên Deque

Các hoạt động được hỗ trợ bởi Deque là:

nối

Phụ lục

Pop và Popleft :- This function is used to add multiple values at the right end of deque. The argument passed is an iterable. Iteratively, it just inserts the values into the deque.

import collections
de = collections.deque([1,2,3])
0


mục lục :- This function is used to add multiple values at the left end of deque. The argument passed is an iterable. Order is reversed as a result of left appends.

import collections
de = collections.deque([1,2,3])
1

chèn :- This function is used to reverse order of deque elements. Just as in other cases for other data-structures.

import collections
de = collections.deque([1,2,3])
2

gỡ bỏ :- This function rotates the deque by the number specified in arguments. If the number specified is negative, rotation occurs to left. By default the rotation is to right.

import collections
de = collections.deque([1,2,3])
3


đếm

import collections
de = collections.deque([1,2,3])
4


gia hạn :- Gives out the length of the Deque.

mở rộng

Câu hỏi

Độ phức tạp về thời gian cho các hoạt động phụ lục và pop?

reverse()

rotate()

append()

Tham khảo phần giới thiệu ở trên.

Phụ lục

Pop và Popleft

Độ phức tạp của thời gian Deque là gì?

Trong việc thực hiện danh sách liên kết gấp đôi và giả sử không có chi phí phân bổ/giao dịch, độ phức tạp của tất cả các hoạt động deque là O (1).Ngoài ra, độ phức tạp về thời gian của việc chèn hoặc xóa ở giữa, được lặp lại, là O (1);Tuy nhiên, độ phức tạp thời gian của truy cập ngẫu nhiên theo chỉ mục là O (n).O(1). Additionally, the time complexity of insertion or deletion in the middle, given an iterator, is O(1); however, the time complexity of random access by index is O(n).

Deque có nhanh hơn danh sách Python không?

Tất nhiên, nó có thể được thực hiện bằng cách sử dụng danh sách, nhưng với Deques, bạn đã có giao diện cho điều đó và nó nhanh hơn nhiều.it's much faster.

Độ phức tạp về thời gian của tập hợp :: chèn là gì?

Độ phức tạp về thời gian của nó là O (logn) trong đó n là kích thước của tập hợp.Chèn (): Chèn một phần tử mới.Độ phức tạp về thời gian của nó là O (logn) trong đó n là kích thước của tập hợp.Kích thước (): Trả về kích thước của tập hợp hoặc số lượng phần tử trong tập hợp.O(logN) where N is the size of the set. insert(): insert a new element. Its time complexity is O(logN) where N is the size of the set. size(): Returns the size of the set or the number of elements in the set.

Deque có nhanh không?

Deques là các loại dữ liệu giống như trình tự được thiết kế như là một khái quát của các ngăn xếp và hàng đợi.Chúng hỗ trợ các hoạt động tăng cường bộ nhớ và nhanh chóng trên cả hai đầu của cấu trúc dữ liệu.They support memory-efficient and fast append and pop operations on both ends of the data structure.