Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed May 21, 2024
1 parent c396174 commit f4c1c76
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions leetcode/78.Subsets/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://leetcode.com/problems/subsets/description/

package main

import "fmt"

func subsets(nums []int) [][]int {
n := len(nums)
totalSubsets := 1 << n // 2^n subsets
result := make([][]int, 0, totalSubsets)

for subsetMask := 0; subsetMask < totalSubsets; subsetMask++ {
subset := []int{}
for i := 0; i < n; i++ {
// Check if the i-th bit in subsetMask is set
if subsetMask&(1<<i) != 0 {
subset = append(subset, nums[i])
}
}
result = append(result, subset)
}

return result
}

func main() {
nums := []int{1, 2, 3}
subsets := subsets(nums)
for _, subset := range subsets {
fmt.Println(subset)
}
}

0 comments on commit f4c1c76

Please sign in to comment.