78.Subsets
生活随笔
收集整理的這篇文章主要介紹了
78.Subsets
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Given a set of distinct integers,?nums, return all possible subsets.
Note:?The solution set must not contain duplicate subsets.
For example,
If?nums?=?[1,2,3], a solution is:
昨天中秋加上頭非常痛,歇了一天。早上起來看了個(gè)cse學(xué)生的新聞,又滾回被窩里睡了好久,準(zhǔn)備再歇一天的。打開看了看一畝三分地,別人99道題
就能刷進(jìn)linkedin,雖然基礎(chǔ)可能差蠻多但還是非常想去投一投簡(jiǎn)歷試試的。過了九月就大批招聘來了,一定要抓緊時(shí)間刷刷題看看書。沒有書
上知識(shí)的支撐只看公開課視頻感覺還是很虛沒底。
思路:dfs+backtracking。對(duì)節(jié)點(diǎn)的操作需要注意:1.碰到節(jié)點(diǎn)就記錄到result:即為pos<=nums.length 2.dps的方向,盡頭(回溯點(diǎn)):pos==nums.length
每一層的操作用pos來記錄,防重復(fù)。
畫graph可以深入理解不同的dps,各種的對(duì)節(jié)點(diǎn)操作。
public class Solution {public List<List<Integer>> subsets(int[] nums) {List<List<Integer>> res=new ArrayList<>();List<Integer> check=new ArrayList<>();dps(nums,res,check,0);return res;}public void dps(int[] nums,List<List<Integer>> res,List<Integer> list,int pos){if(pos<=nums.length){res.add(new ArrayList<Integer>(list));}if(pos==nums.length){return;}for(int i=pos;i<nums.length;i++){list.add(nums[i]);dps(nums,res,list,++pos);list.remove(list.size()-1);}}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/Machelsky/p/5877871.html
總結(jié)
以上是生活随笔為你收集整理的78.Subsets的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于EasyNVR二次开发实现业务需求:
- 下一篇: Hibernate---架构