题目
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],
]
思路
典型的递归回溯法。
(1)递归一次,填入一个数字
(2) 填入的数字,不能是小于当前数字的值,防止重复
(3 )回溯:记得pop_back()最后加上的一个数字,回溯到上一层。
(4) 结束条件:填写够了k个数字的时候,当前填写完毕,回溯
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > result;
vector<int> path;
DFS(n,k,result,path,1);
return result;
}
private:
void DFS(int n,int k,vector<vector<int> > &result,vector<int> &path,int index){
if(path.size() == k){
result.push_back(path);
return;
}
for(int i = index;i <= n;++i){
path.push_back(i);
DFS(n,k,result,path,i+1);
path.pop_back();
}
}
};
int main(){
Solution s;
int n = 5;
int k = 2;
vector<vector<int> > result = s.combine(n,k);
for(int i = 0;i < result.size();++i){
for(int j = 0;j < k;++j){
cout<<result[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
运行时间
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most ...
2. combinations获取所有的组合情况,permutations可以获取所有的排列情况 3. 限制时间范围 4. 转化为字符型
https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 题目描述 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could ...
Combinations of a Phone Number Medium 回溯、暴力 0034 Find First and Last Position of Element in Sorted Array Medium 二分 0039 Combination Sum Medium 回溯 0040 Combination Sum II Medium 回溯 0046 ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
leetcode 316 Every Day Leetcode This project is created to record the ...leetcode. ...设置一个辅助栈专门用于放置当前最小的值,每次压栈时主栈压入新数据,辅助栈压入当前最小数据 ...Combinations
leetcode 刷题笔记 记录一些刷题细节,很惭愧只做了一点微小的工作 4.13 162题. Find Peak Element.Binary search,需要比较nums[mid]和nums[mid+1]. 4.12 212题. Word Search II. 用trie tree存word list,然后dfs. ...
题目来源:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number 题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 ...
leetcode 2 LeetCode - 30 Days 前言 相信所有写程式的人在面试前,总是在揣测在白板题会被问到什么问题,而我们最常听到的准备方式就是“刷”。上有数百个可能是面试官问你...Combinations and Permutations 目录 ref:
Combinations of a Phone Number backtracking int solution[MAX_DIMENSION]; void Backtracking(int dimension) { if(solution is well-generated) { process solution return; } for( x = each value of current ...
Combinations of a Phone Number 093:Restore IP Addresses 树的遍历问题也可以用这种思想来解释。只不过是特殊的递归而已。(只有两路,不用循环) 题型二:动态规划(要整理搜索和DP的区别,都可以用一个状态转移...
leetcode 分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, ...
Combinations of a Phone Number 电话号码的字母组合 回溯法,递归 / Backtrack,Recursion 回溯+递归 Daily Challenge 2020/08/26 20 Valid Parentheses 有效的括号 Stack / 栈 用栈实现配对 Daily Challenge 2020/...
combinations, permutations import random import itertools import collections from fractions import Fraction from collections import Counter import operator import re from functools import reduce from ...
leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 英文题目 题目描述 JavaScript 难度 1 Two ...Combinations of a Phone Number DFS 中等 18 4Sum 中等 19 Remo
Combinations of a Phone Number: 这题是要找到号码对应字符串的所有组合。用字典来表示数字到字符串的组合。然后遍历数字串。使其对应的字母list与前面已有的组合进行 连接即可。 19.Remove Nth Node From End of ...
java lru leetcode Leetcode 问题的解决方案 问题 解决方案 0001_Two_Sum 0002_Add_Two_Numbers 0003_Longest_Substring_Without_Repeating_Characters ...0004_Median_of_Two_...0017_Letter_Combinations_of_a_Phone_N
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持...Combinations of a Phone Number 018 4Sum 020 Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat
Combinations (medium) --locked √ 357 Count Numbers with Unique Digits(medium) 547 Friend Circles (medium) 51 N-Queens (hard) 132 Palindrome Partitioning (hard) 212 Word Search II (hard) DFS /二叉树 ...
leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder ...