Leetcode-combinations
来源:互联网 发布:淘宝冲销量网站 编辑:程序博客网 时间:2024/06/05 10:08
回溯!!!
题目描述
Given two integers n and k, return all possible combinations ofk 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],]
回溯是我觉得最难的题目,因为我无法理清楚里面经历的步骤。今天一并边看题变总结。
import java.util.*;public class Solution { public ArrayList<ArrayList<Integer>> combine(int n, int k) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(k == 0) return result; ArrayList<Integer> track = new ArrayList<Integer>(); back(result, track, 1, n, k); return result; } public void back(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> track, int m , int n, int k) { if(track.size() == k) { ArrayList<Integer> t = new ArrayList<Integer>(track); result.add(t); return; } for(int i=m; i<=n; i++) { track.add(i); back(result, track, i+1, n, k); track.remove(track.size()-1); } }}
0 0
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [LeetCode] Combinations
- Leetcode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [Leetcode] Combinations
- [leetcode] Combinations
- [LeetCode]Combinations
- [leetcode]Combinations
- LeetCode-Combinations
- [leetcode] Combinations
- LeetCode - Combinations
- 【Leetcode】Combinations
- Leetcode: Combinations
- 【leetcode】Combinations
- 【stm32f103-学习笔记】测试看门狗
- 二次指数平滑法求预测值的Java代码
- 第一篇博客
- CSS 实现 系统登录界面 (二)
- CodeChef COT5 线段树
- Leetcode-combinations
- 过拟合的原因+处理方法
- 统计
- 最少拦截系统 hd 1257
- 【NOIP2016提高A组8.11】自然数
- 虚拟机中 共享网络, 桥接网络,NAT,host-only 几种网络连接方式
- 理解DOM事件流的三个阶段
- 第八天 平衡二叉树
- C# 装箱和拆箱