LeetCode 题解(165): Combinations
来源:互联网 发布:ui切图软件 编辑:程序博客网 时间:2024/06/05 00:57
题目:
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],]
题解:
Backtracing。
C++版:
class Solution {public: vector<vector<int>> combine(int n, int k) { vector<vector<int>> results; if(n < k || n == 0 || k == 0) return results; vector<int> result; for(int i = 1; i <= n; i++) { result.push_back(i); combines(results, result, i + 1, n, k - 1); result.pop_back(); } return results; } void combines(vector<vector<int>>& results, vector<int>& result, int i, int n, int k) { if(k == 0) { results.push_back(result); return; } for(int j = i; j <= n; j++) { result.push_back(j); combines(results, result, j + 1, n, k - 1); result.pop_back(); } }};
Java版:
public class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> results = new ArrayList<List<Integer>>(); if(n < k || n == 0 || k == 0) return results; List<Integer> result = new ArrayList<>(); for(int i = 1; i <= n; i++) { result.add(i); combines(results, result, i + 1, n, k - 1); result.clear(); } return results; } public void combines(List<List<Integer>> results, List<Integer> result, int i, int n, int k) { if(k == 0) { List<Integer> temp = new ArrayList<>(); temp.addAll(result); results.add(temp); } for(int j = i; j <= n; j++) { result.add(j); combines(results, result, j + 1, n, k - 1); result.remove(result.size() - 1); } }}
Python版:
import copyclass Solution: # @param {integer} n # @param {integer} k # @return {integer[][]} def combine(self, n, k): if n < k or n == 0 or k == 0: return [] results, result = [], [] for i in range(1, n + 1): result.append(i) self.combines(results, result, i + 1, n, k - 1) result.pop() return results def combines(self, results, result, i, n, k): if k == 0: results.append(copy.copy(result)) return for j in range(i, n + 1): result.append(j) self.combines(results, result, j + 1, n, k - 1) result.pop()
0 0
- LeetCode 题解(165): Combinations
- LeetCode题解:Combinations
- [题解][LeetCode][Combinations]
- LeetCode题解——Combinations
- LeetCode 题解(253) : Factor Combinations
- LeetCode题解:Letter Combinations of a Phone Number
- leetcode 题解 || Letter Combinations of a Phone Number 问题
- LeetCode 题解(77): Letter Combinations of a Phone Number
- LeetCode题解——Letter Combinations of a Phone Number
- LeetCode题解:Letter Combinations of a Phone Number
- 【leetcode题解】【M】【67】Letter Combinations of a Phone Number
- LEETCODE 17 Letter Combinations of a Phone Number (JAVA题解)
- 17. Letter Combinations of a Phone Number LeetCode题解
- Leetcode题解-17. Letter Combinations of a Phone Number
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- POJ---1080-Human Gene Functions
- doget与dopost
- Android eclipse 提示java代码 快捷键
- 【基础练习】【背包DP】codevs1068 乌龟棋题解
- hibernate 4.2.0错误
- LeetCode 题解(165): Combinations
- 九度 Online Judge 算法 刷题 题目1031:xxx定律
- IE8中按下回车键之后自动出现提交
- hdu 5364 Distribution money
- linux系统负载(cat /proc/loadavg)
- 遍历二叉树的各种操作(非递归遍历)
- view 跨activity的动画
- 天声人語
- 读取配置文件的信息