回溯法
HELLO_蓝猫
这个作者很懒,什么都没留下…
展开
-
Leetcode 22. Generate Parentheses
class Solution {public: /* 左括号不超过n个 有左才有右 右括号个数不超过左括号 开头左括号 结尾右括号 */ vector<string> ans; void fun(int n,int m,string &s,int left,int right) //n为回溯法里面的层数 { if (n == (m-1)) { ...原创 2018-05-04 12:47:21 · 122 阅读 · 0 评论 -
Leetcode 113. Path Sum II
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pri...原创 2018-05-08 14:45:52 · 147 阅读 · 0 评论 -
Leetcode 46. Permutations 全排列 标准回溯法
class Solution {public: void fun(vector<vector<int>> &ans, vector<int>& nums, vector<int> &temp,int index) { if (index == nums.size()-1) { temp.push_back...原创 2018-05-16 17:06:32 · 363 阅读 · 0 评论 -
Leetcode 47. Permutations II
class Solution {public: bool repeat(vector<int>& nums, int begin,int j) { for (int i = begin; i < j; i++) { if (nums[i] == nums[j]) { return true; } } return fa...原创 2018-05-16 17:22:15 · 86 阅读 · 0 评论 -
Leetcode 131. Palindrome Partitioning
class Solution{public: void fun(string s,vector<string> &v, vector<vector<string>>&ans) //把其中一个字符串分成两段 看可不可以 { if (s.size() == 1) { v.push_back(s); ans.push_bac...原创 2018-06-21 12:39:20 · 104 阅读 · 0 评论 -
Leetcode 129. Sum Root to Leaf Numbers
解法:回溯法,一个vector去存储每一层的节点,如果是根节点计算结果,反之继续,没用过一个节点要pop出来回溯。Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->...原创 2018-06-14 22:09:35 · 119 阅读 · 0 评论