Chinaunix首页 | 论坛 | 博客
  • 博客访问: 254753
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-09-11 21:58:32

//Subsets My Submissions Question Solution 
//Total Accepted: 62243 Total Submissions: 219925 Difficulty: Medium
//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 = [1,2,3], a solution is:
//
//[
//  [3],
//  [1],
//  [2],
//  [1,2,3],
//  [1,3],
//  [2,3],
//  [1,2],
//  []
//]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Subsets {


public static void main(String[] args) {
// TODO Auto-generated method stub


}
public List<List<Integer>> subsets(int[] nums) {
//先做排序
Arrays.sort(nums);
ArrayList<List<Integer>> result=new ArrayList<>();
int count=nums.length;
result.add(new ArrayList<Integer>());
//把每个元素加进来
for(int i=0;i<count;i++){
int length=result.size();
//不能使用迭代器的方式访问,出现并发修改异常,使用下标方式访问
for(int j=0;j<length;j++){
ArrayList<Integer> other=new ArrayList<>(result.get(j));
other.add(nums[i]);
result.add(other);
}
}
        return result;
    }
}

阅读(233) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~