Hướng dẫn given an integer array nums of unique elements, return all possible subsets python - đã cho một số mảng số nguyên gồm các phần tử duy nhất, trả về tất cả các tập con có thể có python

Đưa ra một loạt các số nguyên ARR [], nhiệm vụ là tìm tất cả các tập hợp con của nó. Tập hợp con không thể chứa các phần tử trùng lặp, do đó, bất kỳ tập hợp con lặp lại chỉ nên được xem xét một lần trong đầu ra.arr[], The task is to find all its subsets. The subset can not contain duplicate elements, so any repeated subset should be considered only once in the output.

Examples:  

Đầu vào: & nbsp; S = {1, 2, 2} đầu ra: & nbsp; {}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2} Giải thích: Tổng số tập hợp của tập hợp đã cho là - {}, {1}, {2} , {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2} ở đây {2} và {1, 2} được lặp lại hai lần để chúng chỉ được coi là một lần đầu ra S = {1, 2, 2}
Output:  {}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2}
Explanation: The total subsets of given set are – {}, {1}, {2}, {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2}
Here {2} and {1, 2} are repeated twice so they are considered only once in the output

Đầu vào: & nbsp; S = {1, 2} đầu ra: & nbsp; {}, {1}, {2}, {1, 2} Giải thích: Tổng số tập hợp của tập hợp đã cho là - {}, {1}, {2}, {1, 2} S = {1, 2}
Output:  {}, {1}, {2}, {1, 2}
Explanation: The total subsets of given set are – {}, {1}, {2}, {1, 2}

Điều kiện tiên quyết: Bộ sức mạnhPower Set

Cách tiếp cận: Dưới đây là ý tưởng giải quyết vấn đề:Below is the idea to solve the problem:

Ý tưởng là sử dụng một mẫu mặt nạ bit để tạo ra tất cả các kết hợp như được thảo luận trong bài. Để tránh in các tập hợp con trùng lặp xây dựng một chuỗi ra khỏi tập hợp con đã cho sao cho các tập hợp con có các phần tử tương tự sẽ dẫn đến cùng một chuỗi. Duy trì một danh sách các chuỗi độc đáo như vậy và cuối cùng giải mã tất cả các chuỗi như vậy để in các yếu tố riêng lẻ của nó.bit-mask pattern to generate all the combinations as discussed in post. To avoid printing duplicate subsets construct a string out of given subset such that subsets having similar elements will result in same string. Maintain a list of such unique strings and finally decode all such string to print its individual elements.

Hình minh họa :

S = {1, 2, 2}

Các chữ số nhị phân từ 0 đến 7 là

0 -> 000 & nbsp; & nbsp; -> Số được hình thành không có setBits & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; -> {} 1 -> 001 & nbsp; & nbsp; -> Số được hình thành với setbit ở vị trí 0 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; -> {1} 2 -> 010 & nbsp; & nbsp; -> số được hình thành với setbit ở vị trí 1 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; -> {2} 3 -> 011 & nbsp; & nbsp; -> Số được hình thành với setbit ở vị trí 0 & nbsp; và 1 & nbsp; & nbsp; -> {1, 2} 4 -> 100 & nbsp; & nbsp; -> số được hình thành với setbit ở vị trí 2 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; -> & nbsp; {2} 5 -> 101 & nbsp; & nbsp; -> số được hình thành với setbit ở vị trí 0 và 2 & nbsp; & nbsp; & nbsp; -> {1, 2} 6 -> 110 & nbsp; & nbsp; -> số được hình thành với setbit ở vị trí 1 và 2 & nbsp; & nbsp; & nbsp; -> {2, 2} 7 -> 111 & nbsp; & nbsp; -> Số được hình thành với setbit ở vị trí 0, 1 và 2 & nbsp; -> {1, 2, 2}
1 –> 001    –> number formed with setbit at position 0                –> { 1 }
2 –> 010    –> number formed with setbit at position 1                –> { 2 }
3 –> 011    –> number formed with setbit at position 0  and 1     –> { 1 , 2 }
4 –> 100    –> number formed with setbit at position 2                –>  { 2 }
5 –> 101    –> number formed with setbit at position 0 and 2      –> { 1 , 2}
6 –> 110    –> number formed with setbit at position 1 and 2      –> { 2 , 2}
7 –> 111    –> number formed with setbit at position 0 , 1 and 2  –> {1 , 2 , 2}

Sau khi xóa các bản sao kết quả cuối cùng sẽ là {}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2}

Lưu ý: Phương thức này sẽ chỉ hoạt động trên các mảng được sắp xếp. & NBSP;This method will only work on sorted arrays. 

Thực hiện theo các bước dưới đây để thực hiện ý tưởng:

  • Khởi tạo một biến pow_set_size khi 2 tăng kích thước mảng và một vectơ của vectơ ANS để lưu trữ tất cả các tập hợp con.pow_set_size as 2 raise to size of array and a vector of vector ans to store all subsets.
  • Lặp lại trên tất cả các bitmasks từ 0 đến pow_set_size & nbsp; - 1.pow_set_size  – 1.
    • Đối với mỗi bitmask bao gồm các phần tử của mảng các chỉ số trong đó các bit được đặt thành một tập hợp con & nbsp; vector.subset vector.
    • Nếu tập hợp con này không tồn tại thì hãy đẩy tập hợp con trong vectơ ANS.push the subset in the ans vector.
  • Trả lại ans.ans.

Dưới đây là việc thực hiện phương pháp trên: & nbsp; & nbsp;

C++14

#include

using namespace std;

vectorint> > findPowerSet(vector<int>& nums)

#include 0

#include 1int #include 3

#include 1#include 5int #include 7#include 8#include 9

#include 1using1

#include 1vectorintusing5

#include 1using7

#include 1using9 namespace0int namespace2

namespace3namespace4

namespace5namespace6intnamespace8

namespace5std;0std;1std;2

namespace5using9 namespace0int std;7

std;8std;9 vector0

vector1vector2

vector1vector4vector5std;2

std;8vector8

namespace5vector8

namespace5std;9 int3

std;8int5

std;8int7

std;8int9

namespace5vector8

#include 1vector8

#include 1> > findPowerSet(vector<5 > > findPowerSet(vector<6

vector8

int > > findPowerSet(vector<9

#include 0

#include 1namespace6intint4

#include 1vectorintint8

#include 1using9 namespace0int >& nums)3

namespace5using9 namespace0int >& nums)8

std;8#include 00#include 01std;2

namespace5#include 04

#include 1vector8

#include 1> > findPowerSet(vector<5 #include 09

vector8

Java

#include 11 #include 12

#include 11 #include 14

#include 15 #include 16 #include 17

#include 1#include 19 #include 20

#include 1#include 0

namespace5#include 29#include 30 #include 31

namespace5#include 33 #include 34#include 33#include 36#include 37#include 38

namespace5int #include 41

namespace5using9 #include 44#include 45#include 46

#include 47namespace4

std;8#include 50std;1std;2

std;8using9 #include 55#include 45#include 57

vector1std;9 #include 60#include 61 #include 62#include 45#include 64

#include 65#include 66

#include 67#include 68vector5#include 70

std;8vector8

std;8std;9 #include 75

vector1#include 77#include 45#include 79

vector1#include 81

std;8vector8

namespace5vector8

namespace5using9 #include 88

std;8#include 90vector5#include 92#include 93#include 70

std;8#include 96

namespace5vector8

#include 1vector8

#include 1#include 15 #include 19 #include 20 using05

#include 1#include 0

Các

namespace5using18using19#include 70

#include 1vector8

vector8

Python3

using24 using25

#include 1using27using28 using29

#include 1using9 using32using33 using34namespace0#include 37using37using37using39

namespace5using41using28 std;1

namespace5using9 using46using33 using34using49

std;8std;9 using522#include 61 using54using28

vector1using41using60using28 using62using63using60 using65

namespace5std;9 using41using69 using33 using27using72

std;8using78

#include 1using9 using41using33 using83

namespace5using85using28 using87using88#include 64

namespace5using9 using92using33 using94

std;8using96using97using28#include 01#include 64

namespace5using96namespace03

std;9 namespace05using28using28 namespace08using57

#include 1using85using28 namespace13using11#include 92using13#include 92using13namespace19

#include 1namespace21using28 using73namespace24

#include 1namespace26

JavaScript

namespace27

namespace5namespace29

std;8namespace31

std;8namespace33

std;8namespace35

std;8namespace37

std;8namespace39

std;8using9 namespace42

vector1namespace44

vector1namespace46std;1std;2

vector1using9 namespace51

#include 65std;9 vector0

#include 67namespace56

#include 67namespace58vector5std;2

#include 65vector8

vector1vector8

vector1std;9 namespace67

#include 65namespace69

#include 65namespace71

vector1vector8

std;8vector8

std;8> > findPowerSet(vector<5 > > findPowerSet(vector<6

namespace5vector8

namespace5namespace82

namespace5namespace84

namespace5using9 namespace87

std;8using9 namespace90

vector1namespace92

std;8namespace94namespace95#include 70

namespace5vector8

#include 1std;00

Đầu ra

10 
12 
10 12 
12 12 
10 12 12 

Độ phức tạp về thời gian: O (N*2N) Không gian phụ trợ: O (n*n)O(N*2N)
Auxiliary Space:O(N*N)

Analysis:

If & nbsp; & nbsp; là tổng số bước trong mã, sau đó vòng lặp để tạo tất cả các kết hợp nhị phân chạy cho đến khi vòng lặp bên trong chạy cho đến log (i) .Hence, & nbsp ;, tăng sức mạnh của hai bên ở cả hai bên & nbsp; & nbsp; & nbsp; Sử dụng nhật ký ở cả hai bên và áp dụng xấp xỉ của Sterling, do đó độ phức tạp thời gian là & nbsp;

Hướng dẫn given an integer array nums of unique elements, return all possible subsets python - đã cho một số mảng số nguyên gồm các phần tử duy nhất, trả về tất cả các tập con có thể có python
  is the total number of steps in the code, then the loop to generate all binary combinations runs till, and then the inner loop run till log(i).
Hence, 
Hướng dẫn given an integer array nums of unique elements, return all possible subsets python - đã cho một số mảng số nguyên gồm các phần tử duy nhất, trả về tất cả các tập con có thể có python
, Raising to the power of two on both sides 
 
Hướng dẫn given an integer array nums of unique elements, return all possible subsets python - đã cho một số mảng số nguyên gồm các phần tử duy nhất, trả về tất cả các tập con có thể có python
 
Using log on both sides and applying Sterling’s approximation,
Hướng dẫn given an integer array nums of unique elements, return all possible subsets python - đã cho một số mảng số nguyên gồm các phần tử duy nhất, trả về tất cả các tập con có thể có python

Hence the time complexity is 
Hướng dẫn given an integer array nums of unique elements, return all possible subsets python - đã cho một số mảng số nguyên gồm các phần tử duy nhất, trả về tất cả các tập con có thể có python

Tìm tất cả các tập hợp con riêng biệt của một tập hợp nhất định bằng cách sử dụng phương pháp bitmasking bằng cách sử dụng backtracking

Tham khảo bài viết https://www.geeksforgeek.org/backtracking-to-find-all-subsets/ để giải quyết vấn đề bằng cách sử dụng phương pháp quay lại.

Bài viết này được đóng góp bởi Aditya Goel. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng cách sử dụng PROPTENT.GeekSforGeeks.org hoặc gửi bài viết của bạn đến. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. Xin vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. & NBSP;Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.GeeksforGeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


Làm cách nào để tìm thấy tất cả các tập hợp con của một mảng trong Python?

Python có itertools.combinations (ITable, n) trả về n độ dài của các phần tử từ đầu vào có thể điều chỉnh được. Điều này có thể được sử dụng để in tất cả các tập hợp con có kích thước nhất định của một bộ.itertools. combinations(iterable, n) which Return n length subsequences of elements from the input iterable. This can be used to Print all subsets of a given size of a set.

Làm thế nào để bạn tìm thấy các tập hợp con độc đáo trong một mảng?

Thực hiện theo các bước đã cho để giải quyết vấn đề bằng cách sử dụng phương pháp trên: lặp lại từng yếu tố. Đối với mỗi phần tử, chỉ cần chọn phần tử và di chuyển về phía trước đệ quy và thêm tập hợp con vào kết quả. Sau đó sử dụng backtracking, loại bỏ phần tử và tiếp tục tìm các tập hợp con và thêm chúng vào kết quả.

Làm thế nào để bạn tạo ra tất cả các tập hợp con có thể?

Ở đây chúng tôi đang tạo ra mọi tập hợp con bằng cách sử dụng đệ quy.Tổng số tập hợp con của một bộ kích thước nhất định n = 2^n.Độ phức tạp không gian: O (n) cho tập hợp con mảng thêm ...
Chọn một phần tử từ đầu vào, tức là tập hợp con [len] = s [pos].....
Hình thành tập hợp con đệ quy bao gồm nó, tức là allsubsets (pos+1, len+1, tập hợp con).

Làm thế nào để bạn tạo tập hợp con bằng cách sử dụng thao tác bit?

Thực hiện theo các bước dưới đây để thực hiện ý tưởng: Khởi tạo một biến pow_set_size khi 2 tăng kích thước mảng và vectơ của vectơ ANS để lưu trữ tất cả các tập hợp con.Lặp lại trên tất cả các bitmasks từ 0 đến pow_set_size - 1. Đối với mỗi bitmask bao gồm các phần tử của mảng các chỉ số trong đó các bit được đặt thành một vectơ tập hợp con.