77. 组合 - 力扣(LeetCode)
class Solution {ArrayList<Integer> path = new ArrayList<>();ArrayList<List<Integer>> result = new ArrayList<>();public List<List<Integer>> combine(int n, int k) {if(n < k){return new ArrayList<>();}doCombine(n,k,1);return result;}private void doCombine(int n,int k,int startPos){if(path.size() == k){result.add(new ArrayList<>(path));return;}for(int i=startPos;i<=n && path.size() + n-i+1 >= k;i++){path.add(i);doCombine(n,k,i+1);path.remove(path.size()-1);}}
}
使用面向接口 和 不使用面向接口