日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Leetcode][第78题][JAVA][子集][位运算][回溯]

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leetcode][第78题][JAVA][子集][位运算][回溯] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【問題描述】[中等]

【解答思路】

1. 位運算

復雜度

class Solution {List<Integer> t = new ArrayList<Integer>();List<List<Integer>> ans = new ArrayList<List<Integer>>();public List<List<Integer>> subsets(int[] nums) {int n = nums.length;for (int mask = 0; mask < (1 << n); ++mask) {t.clear();for (int i = 0; i < n; ++i) {if ((mask & (1 << i)) != 0) {t.add(nums[i]);}}ans.add(new ArrayList<Integer>(t));}return ans;} }
2. 回溯 begin標記

class Solution {List<Integer> t = new ArrayList<Integer>();List<List<Integer>> ans = new ArrayList<List<Integer>>();public List<List<Integer>> subsets(int[] nums) {dfs(0, nums);return ans;}public void dfs(int cur, int[] nums) {if (cur == nums.length) {ans.add(new ArrayList<Integer>(t));return;}t.add(nums[cur]);dfs(cur + 1, nums);t.remove(t.size() - 1);dfs(cur + 1, nums);} }

【總結】

1. 什么時候用或者不用used數組?

2.位運算相關

3.回溯相關題目

[Leedcode][JAVA][第46題][全排列][回溯算法]
[Leetcode][第81題][JAVA][N皇后問題][回溯算法]
[Leetcode][第60題][JAVA][第k個排列][回溯][DFS][剪枝]
[Leetcode][第39題][JAVA][組合總和][回溯][dfs][剪枝]
[Leetcode][第40題][JAVA][數組總和2][回溯][剪枝]
[Leetcode][第216題][JAVA][數組之和3][回溯]
[Leetcode][第79題][JAVA][單詞搜索][DFS][回溯]
[Leetcode][第17題][JAVA][電話號碼的字母組合][回溯]
[Leetcode][第93題][JAVA][復原IP地址][剪枝][回溯]
[Leetcode][第679題][JAVA][24點游戲][回溯][暴力]

轉載鏈接:https://leetcode-cn.com/problems/subsets/solution/zi-ji-by-leetcode-solution/

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的[Leetcode][第78题][JAVA][子集][位运算][回溯]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。