hard Array

Subsets

Write a function subsets that takes an array of distinct integers and returns all possible subsets.

Function Signature

function subsets(nums);

Example 1:

Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Explanation: The subsets are [[] [1] [2] [1,2] [3] [1,3] [2,3] [1,2,3]].

Example 2:

Input: nums = [0]
Output: [[],[0]]
Explanation: The subsets are [[] [0]].

Solve this problem

Start your 7-day free trial to solve this problem in our code editor with instant feedback.

Start 7-Day Free Trial to Solve This Problem

Related Problems

Search in Rotated Sorted Array

hard

Write a function `search` that takes a rotated sorted array and a target value, and returns the index if the target is found. If not, returns -1. ## Function Signature ```function search(nums, target);``` ### Example 1: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0` \ > **Output:** `4` \ > **Explanation:** Target 0 is at index 4. ### Example 2: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3` \ > **Output:** `-1` \ > **Explanation:** Target 3 is not in the array.

Array

Top K Frequent Elements

hard

Write a function `topKFrequent` that takes an array of integers and an integer k, and returns the k most frequent elements. ## Function Signature ```function topKFrequent(nums, k);``` ### Example 1: > **Input:** `nums = [1,1,1,2,2,3]`, `k = 2` \ > **Output:** `[1,2]` \ > **Explanation:** The two most frequent elements are 1 and 2. ### Example 2: > **Input:** `nums = [1]`, `k = 1` \ > **Output:** `[1]` \ > **Explanation:** The most frequent element is 1.

Array

Permutations

hard

Write a function `permute` that takes an array of distinct integers and returns all possible permutations. ## Function Signature ```function permute(nums);``` ### Example 1: > **Input:** `nums = [1,2,3]` \ > **Output:** `[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]` \ > **Explanation:** The permutations of [1,2,3] are [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]. ### Example 2: > **Input:** `nums = [0,1]` \ > **Output:** `[[0,1],[1,0]]` \ > **Explanation:** The permutations of [0,1] are [[0,1],[1,0]]. ### Example 3: > **Input:** `nums = [1]` \ > **Output:** `[[1]]` \ > **Explanation:** The permutations of [1] are [[1]].

Array