Subsets
2015-07-12 04:19
183 查看
Given a set of distinct integers, nums, 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 nums =
a solution is:
Java:
public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
ArrayList<ArrayList<Integer>> listinlist = new ArrayList<ArrayList<Integer>>();
for(int k=0; k<S.length; k++)
{
int m = listinlist.size();
for(int i = 0; i< m; i++)
{
ArrayList<Integer> ltemp = listinlist.get(i);
ArrayList<Integer> ladd = new ArrayList<Integer>();
Boolean added = false;
for(int j = 0; j < ltemp.size(); j++)
{
if(ltemp.get(j) > S[k] && !added)
{
ladd.add(S[k]);
added = true;
}
ladd.add(ltemp.get(j));
}
if(!added)
ladd.add(S[k]);
listinlist.add(ladd);
}
ArrayList l1 = new ArrayList();
l1.add(S[k]);
listinlist.add(l1);
}
ArrayList<Integer> l2 = new ArrayList<Integer>();
listinlist.add(l2);
return listinlist;
}
}
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums =
[1,2,3],
a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Java:
public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
ArrayList<ArrayList<Integer>> listinlist = new ArrayList<ArrayList<Integer>>();
for(int k=0; k<S.length; k++)
{
int m = listinlist.size();
for(int i = 0; i< m; i++)
{
ArrayList<Integer> ltemp = listinlist.get(i);
ArrayList<Integer> ladd = new ArrayList<Integer>();
Boolean added = false;
for(int j = 0; j < ltemp.size(); j++)
{
if(ltemp.get(j) > S[k] && !added)
{
ladd.add(S[k]);
added = true;
}
ladd.add(ltemp.get(j));
}
if(!added)
ladd.add(S[k]);
listinlist.add(ladd);
}
ArrayList l1 = new ArrayList();
l1.add(S[k]);
listinlist.add(l1);
}
ArrayList<Integer> l2 = new ArrayList<Integer>();
listinlist.add(l2);
return listinlist;
}
}
相关文章推荐
- Scheme 4 Javaer导航
- 优秀程序员如养成
- IQ Test
- Java操作文本文件的方法详解
- 字符串(2)---使用正则表达式
- 字符串
- 圆形和矩形的碰撞检测
- 添加MFC类时,从基类中找不到CRecordSet的解决办法
- VC 在对话框中使用定时器
- 语音识别技术的最新进展
- Android 开发工具有哪些?
- VC程序调用MATCOM导致进程不自动结束的解决办法
- project小技巧:快捷键
- project小技巧:快捷键
- Web前端和后端之区分
- 安卓获取手机内存,SD卡内存使用状态的方法
- win7旗舰版下怎么配置ODBC数据源
- 普通美国人的词汇量
- HTTP 笔记与总结(4 )socket 编程:批量发帖
- 安卓读写文件和路劲注意事项