已解决
代码随想录算法训练营第二十八天| 78 子集 90 子集|| 93 复原IP地址
来自网友在路上 179879提问 提问时间:2023-11-20 05:04:30阅读次数: 79
最佳答案 问答题库798位专家为你答疑解惑
78 子集
由题意可知数组中的元素互不相同,所以在dfs中我们可以将当前的path直接加入到res中。
class Solution {List<List<Integer>>res = new ArrayList<>();List<Integer>path = new LinkedList<>();public List<List<Integer>> subsets(int[] nums) {dfs(0,nums);return res;}private void dfs(int cnt,int[] nums){res.add(new LinkedList(path));for(int i = cnt;i < nums.length;i++){path.add(nums[i]);dfs(i + 1,nums);path.remove(path.size() - 1);}}
}
时间复杂度O(n×)
空间复杂度O(n)
90 子集||
class Solution {List<List<Integer>>res = new ArrayList<>();List<Integer>path = new LinkedList<>();public List<List<Integer>> subsetsWithDup(int[] nums) {Arrays.sort(nums);dfs(0,nums);return res;}private void dfs(int cnt,int nums[]){res.add(new LinkedList(path));for(int i = cnt;i < nums.length;i++){if(i > cnt && nums[i] == nums[i - 1])continue;path.add(nums[i]);dfs(i + 1,nums);path.remove(path.size() - 1);}}
}
时间复杂度O(n×)
空间复杂度O(n)
93 复原IP地址
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"代码随想录算法训练营第二十八天| 78 子集 90 子集|| 93 复原IP地址":http://eshow365.cn/6-40006-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 【NGINX--1】基础知识
- 下一篇: fetch 获取流式数据(chatgpt的流式输出)