In this tutorial, we are going to solve a leetcode problem, subsets in python.
Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Input: nums = [0]
Output: [[],[0]]
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
lst = [[]]
for i in range(1,len(nums)+1):
s = itertools.combinations(nums, i)
for j in s:
lst.append(list(j))
return lst
step1: In this problem, first we declare a list that will contain lists of subsets.
step2: Now, we will loop into the nums array from index 1 to len(nums) + 1.
step3: Now as per the advantage of python we'll be going to use the itertools function combination which will take an array and an integer i to return a list of subsets of size i.
step4: Then, we loop over the returned list from the itertools function and append it to our original lst list and after the loop, we return lst.
Trainings :
Data Science Training in Indore | Python Training in Indore | Data Analytics Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Dart | OpenCV Tutorials | Projects | Interview Questions | Python Data Structures and Algorithms | Aptitude Tests | Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | HackerRank Python | Pandas Tutorials | Projects | Interview Questions | Rust Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | MongoDB Tutorials | Examples | Interview Questions | HackerRank C Program Solutions | Python Tutorials by CodersDaily | React.js Tutorials | Golang Tutorials | Projects | Interview Questions | MS Sql Server Tutorials | Examples | Interview Questions | Verbal Ability Tutorial | Numpy Tutorials | Projects | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Tensor Flow | HackerRank C++ Solutions | Django REST Framework Tutorial | CodeChef Python Solutions | Leetcode Python Solutions | Reasoning Ability Tutorial | Quantitative Ability Tutorial | C++ Tutorials | HackerRank Java Solutions | TCS NQT Mock Test Series | Verbal Aptitude 2 | HackerRank DSA Solutions | HackerRank SQL Solutions | Javascript | Node.js Tutorials |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |