JAVA版《集合的所有子集》
集合的所有子集
http://www.nowcoder.com/questionTerminal/c333d551eb6243e0b4d92e37a06fbfc9
回溯算法
import java.util.*; public class Solution { ArrayList<ArrayList<Integer>> res = new ArrayList<>(); public ArrayList<ArrayList<Integer>> subsets(int[] S) { LinkedList<Integer> track = new LinkedList<>(); back(S, 0, track); return res; } public void back(int[] S, int start, LinkedList<Integer> track){ res.add(new ArrayList<>(track)); for(int i = start; i < S.length; i++){ track.add(S[i]); back(S, i+1, track); //撤销 回溯 track.removeLast(); } } }