We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
出处:LeetCode 算法第77题 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
出处:LeetCode 算法第77题
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
还是采取深度优先遍历,当组合的个数达到要求时存入结果中
var combine = function (n, k) { var result = []; var temp = []; DFS(result, temp, 1, n + 1, k); return result; }; function copy(array) { var result = []; for (var i = 0, len = array.length; i < len; i++) { result.push(array[i]); } return result; } function DFS(result, temp, index, n, k) { if (temp.length == k) { result.push(copy(temp)); return; } if (index < n) { for (var p = index; p < n; p++) { temp.push(p); DFS(result, temp, p + 1, n, k); temp.pop(); } } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
习题
思路
还是采取深度优先遍历,当组合的个数达到要求时存入结果中
解答
The text was updated successfully, but these errors were encountered: