Swift Subsets Algorithm: Generating All Subsets of an Array
class Solution { var t = Int var ans = [Int]
func subsets(_ nums: [Int]) -> [[Int]] {
let n = nums.count
for mask in 0..<(1 << n) {
t.removeAll()
for i in 0..<n {
if (mask & (1 << i)) != 0 {
t.append(nums[i])
}
}
ans.append(t)
}
return ans
}
}
原文地址: https://www.cveoy.top/t/topic/qr57 著作权归作者所有。请勿转载和采集!