Skip to content

Latest commit

 

History

History
39 lines (33 loc) · 673 Bytes

README.md

File metadata and controls

39 lines (33 loc) · 673 Bytes

78. Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Solutions (Rust)

1. Solution

impl Solution {
    pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
        let mut ret = vec![vec![]];

        for num in nums {
            let mut temp = ret.clone();
            temp.iter_mut().for_each(|x| x.push(num));
            ret.append(&mut temp);
        }

        ret
    }
}