三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

LeetCode HOT100 - 子集

LeetCode HOT100 - 子集

回溯搜索

每个位置选或不选

class Solution {
public:vector<vector<int>> subsets(vector<int>& a) {int n = a.size();vector<vector<int>> ans;vector<int> tmp;auto dfs = [&](this auto&& self, int it) -> void{if (it == n) {ans.emplace_back(tmp);return;}self(it + 1);tmp.emplace_back(a[it]);self(it + 1);tmp.pop_back();};dfs(0);return ans;}
};
← 返回列表