标签:style blog class code java c
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a
solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
public class Solution { public ArrayList<ArrayList<Integer>> combine(int n, int k) { if(k>n) return null; ArrayList<Integer> ai = new ArrayList<Integer>(); ArrayList<ArrayList<Integer>> re = new ArrayList<ArrayList<Integer>>(); for(int m=0;m<n;m++){ ai=new ArrayList<Integer>(); ai.add(m+1); re.add(ai); } for(int i=1;i<k;i++){ Iterator<ArrayList<Integer>> it = re.iterator(); ArrayList<ArrayList<Integer>> tempre = new ArrayList<ArrayList<Integer>>(); while(it.hasNext()){ ArrayList<Integer> temp = it.next(); int tt = temp.get(temp.size()-1); for(int j=tt+1;j<=n;j++){ ArrayList<Integer> newtemp = new ArrayList<Integer>(); newtemp.addAll(temp); newtemp.add(j); tempre.add(newtemp); } } re=tempre; } return re; } }
【LeetCode】Combinations,布布扣,bubuko.com
标签:style blog class code java c
原文地址:http://www.cnblogs.com/yixianyixian/p/3721944.html