Skip to content

78. Subsets #53

Answered by mah-shamim
mah-shamim asked this question in Q&A
Jul 17, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

Let's implement this solution in PHP: 78. Subsets

<?php
function subsets($nums) {
        $ans = [[]];
        foreach($nums as $num){
            $ans_size = count($ans);
            for($i = 0; $i < $ans_size; $i++){
                $cur = $ans[$i];
                $cur[] = $num;
                $ans[] = $cur;
            }
        }
        return $ans;
    }
// Test the function with example inputs
print_r(subsets([1,2,3])); // Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
print_r(subsets([0])); // Output: [[],[0]]
?>

Explanation:

  1. Function Signature and Input:

    • The function subsets() takes an array of integers nums as input and …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jan 23, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants