leetcode78. 子集(回溯)
生活随笔
收集整理的這篇文章主要介紹了
leetcode78. 子集(回溯)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一組不含重復元素的整數數組 nums,返回該數組所有可能的子集(冪集)。
說明:解集不能包含重復的子集。
示例:
輸入: nums = [1,2,3]
輸出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
代碼
class Solution {public List<List<Integer>> subsets(int[] nums) {sets(nums,0,new ArrayList<>());return ress;}public void sets(int[] nums,int start,List<Integer> temp) {ress.add(new ArrayList<>(temp)); //將上一層的路徑加入for(int i=start;i<nums.length;i++)//可以選擇的數字{temp.add(nums[i]);sets(nums, i+1, temp);temp.remove(temp.size()-1);//回溯}}List<List<Integer>> ress=new ArrayList<>(); }總結
以上是生活随笔為你收集整理的leetcode78. 子集(回溯)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到被鬼纠缠是什么意思
- 下一篇: leetcode89. 格雷编码