본문 바로가기
알고리즘

[Swift] LeetCode - 78. Subsets

by 고고 2022. 2. 16.

문제: https://leetcode.com/problems/subsets/

 

Subsets - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

import Foundation

class Solution {
    func subsets(_ nums: [Int]) -> [[Int]] {
        var result = [[Int]]()
        
        func combination(_ index: Int, _ nowCombi: [Int]) {
            result.append(nowCombi)
            
            if nowCombi.count == nums.count {
                return
            }
            
            for i in index..<nums.count {
                combination(i + 1, nowCombi + [nums[i]])
            }
        }
        
        combination(0, [])
        
        return result
    }
}

댓글