Leetcode题解(25)

77. Combinations

题目

分析:求给定数字n,k的组合数,方法是采用深度搜索算法,代码如下(copy网上代码)

 1 class Solution {
 2 public:
 3     void dfs77(vector<vector<int > > &ans, vector<int> subans, int start, int n, int k)
 4     {
 5         if (subans.size() == k)
 6         {
 7             ans.push_back(subans); return ;
 8         }
 9         for (int i = start; i <= n; i++)
10         {
11             subans.push_back(i);
12             dfs77(ans, subans, i + 1, n, k);
13             subans.pop_back(); // 满足一个条件或者该分支递归完后要删除最后一个
14         }
15     }
16     vector<vector<int> > combine(int n, int k) {
17         vector<vector<int > > ans;
18         if (n < k || k == 0) return ans;
19         vector<int> subans;
20         dfs77(ans, subans, 1, n, k);
21         return ans;
22     }
23 };

--------------------------------------------------------------------------------分割线------------------------------------------------------------------

78. Subsets

题目

分析:求一个集合的所有子集,代码如下(copy网上代码)

 1 class Solution {
 2 public:
 3     vector<vector<int>>res;
 4     vector<int>ans;
 5     vector<vector<int>> subsets(vector<int>& nums) {
 6         if(nums.empty()) return res;
 7         sort(nums.begin(), nums.end());
 8         dfs(0, ans, nums);
 9         return res;
10
11     }
12     void dfs(int k, vector<int>ans, vector<int> nums){
13         res.push_back(ans);
14         for(int i = k; i < nums.size(); i++){
15             ans.push_back(nums[i]);
16             dfs(i + 1, ans, nums);
17             ans.pop_back();
18         }
19     }
20 };
时间: 2024-10-27 04:18:56

Leetcode题解(25)的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co