
DFS
KRYON!
这个作者很懒,什么都没留下…
展开
-
[leetcode]1367. 二叉树中的列表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /** * Definition for a binary tree n...原创 2020-03-02 22:18:49 · 384 阅读 · 0 评论 -
[leetcode]376. 摆动序列
给定一个整数序列,返回作为摆动序列的最长子序列的长度,通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序 这个删除是指删除上升后又接着上升的元素 1、暴力:超时 2、动态规划: up[i]是目前为止最长的以第i个元素结尾上升的摆动序列的长度。 down[i]是目前为止最长的以第i个元素结尾下降的摆动序列的长度。 class Solution { publ...转载 2020-02-21 22:36:40 · 157 阅读 · 0 评论 -
[leetcode] 140. 单词拆分 II
class Solution { int n; vector<string>res; unordered_map<int,set<int>>hash; string s; void dfs(int start, string temp) { if(start == n) { ...原创 2020-02-19 22:41:08 · 128 阅读 · 0 评论 -
[leetcode]139. 单词拆分
1、递归+备忘录 class Solution { string s; vector<string> wordDict; unordered_set<string>m_set; vector<int>memo;//memo[i]表示[i, size()-1]可不可拆分若干个单词连着 1可 0不可 -1不知道 int d...原创 2020-02-19 16:24:25 · 333 阅读 · 0 评论 -
[leetcode]78. 子集
DFS: class Solution { public: vector<vector<int>>ans; vector<vector<int>> subsets(vector<int>& nums) { int n = nums.size(); vector<int>...原创 2020-02-15 14:38:21 · 176 阅读 · 0 评论 -
[leetcode]980. 不同路径 III
DFS class Solution { int r,c; int startX,startY; int endX, endY; int res = 0; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; vector<vector<int>> grid; ...原创 2020-02-14 23:03:56 · 207 阅读 · 0 评论 -
[leetcode]102. 二叉树的层次遍历
BFS /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */...原创 2020-02-06 21:56:58 · 117 阅读 · 0 评论 -
[leetcode]1306. 跳跃游戏 III
DFS解法 class Solution { vector<bool>visited; vector<int>arr; int len; public: bool DFS(int pos) { if(arr[pos] == 0) { return true; ...原创 2019-12-30 21:03:21 · 300 阅读 · 1 评论 -
[leetcode]529. 扫雷游戏 DFS递归、BFS、DFS栈实现
https://leetcode-cn.com/problems/minesweeper/ DFS步骤: 1、给的x,y坐标位置是地雷board[x][y] == 'M',把那个位置标记为地雷·board[x][y] = ‘X’,返回。 2、不是地雷board[x][y]=='E',检查下这个坐标相邻位置有没有地雷: 没有,标记当前位置board[x][y]='B',8个方向递归; 有,...原创 2019-12-23 14:58:44 · 259 阅读 · 0 评论 -
377. 组合总和 Ⅳ
class Solution { public: int combinationSum4(vector<int>& nums, int target) { if(nums.size() == 0 || target <= 0) { return 0; } vector<...原创 2019-12-06 19:23:35 · 142 阅读 · 0 评论 -
216. 组合总和 III
class Solution { private: vector<vector<int> >res; vector<int>path; void DFS(int start, int k, int pos, int n, int sum) { if(k == pos) //有三个了 { ...原创 2019-12-06 19:06:39 · 116 阅读 · 0 评论 -
leetcode--组合总数
1、剪枝+回溯: class Solution { private: vector<int> candidates; vector<vector<int>> res; vector<int>path; void DFS(int start, int target) { if(target ==...原创 2019-12-06 18:37:35 · 200 阅读 · 0 评论