Given a set of distinct integers, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S =
If S =
[1,2,3]
, a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
public class Solution { public ArrayList<ArrayList<Integer>> subsets(int[] S) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(S.length== 0 || S== null){ return result; } ArrayList<Integer> tem = new ArrayList<Integer>(); Arrays.sort(S); dfs(result,tem,S,0); return result; } private void dfs(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> tem,int[] S,int pos){ result.add(new ArrayList<Integer>(tem)); for(int i = pos; i < S.length; i++){ tem.add(S[i]); dfs(result,tem,S,i+1); tem.remove(tem.size()-1); } } }
1 java中的length属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了length这个属性.
2 java中的length()方法是针对字符串String说的,如果想看这个字符串的长度则用到length()这个方法.
3.java中的size()方法是针对泛型集合说的,如果想看这个泛型有多少个元素,就调用此方法来查看!
这个例子来演示这两个方法和一个属性的用法
public static void main(String[] args) {
String []list={"ma","cao","yuan"};
String a="macaoyuan";
System.out.println(list.length);
System.out.println(a.length());
List<Object> array=new ArrayList();
array.add(a);
System.out.println(array.size());
}
String []list={"ma","cao","yuan"};
String a="macaoyuan";
System.out.println(list.length);
System.out.println(a.length());
List<Object> array=new ArrayList();
array.add(a);
System.out.println(array.size());
}
输出的值为:
3
9
1