240
笔下虽有千言,胸中实无一策
上一页 1 2 3 4 5 6 ··· 17 下一页
摘要: 题解 Medium Tree, Stack 几天之内再次做了一遍,思路依旧会卡壳。看来在栈的运用上还是一个难点,毕竟这个倒序的数据结构比起队列并不如那么直观。不过还是很重要。 思路就是先把左节点依次压入栈中,直到最左边的节点,那么以当前节点为根节点,排序就比较容易了,这和遍历方向是一致的所以,可以把 阅读全文
posted @ 2020-10-08 06:47 CasperWin 阅读(56) 评论(0) 推荐(0) 编辑
摘要: 题解 Medium BFS /* // Definition for a Node. class Node { public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _va 阅读全文
posted @ 2020-10-06 13:21 CasperWin 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 题解 双指针 三次遍历,但不影响复杂度为O(n)。后面要进行改进。 class Solution { public: int trap(vector<int>& height) { vector<int> max_left(height.size()+2, 0); vector<int> max_r 阅读全文
posted @ 2020-10-03 07:36 CasperWin 阅读(79) 评论(0) 推荐(0) 编辑
摘要: 题解 Easy Stack class Solution { public: bool isValid(string s) { stack<char> st; int i = 0; while(i < s.size()) { if(s[i] == '(' || s[i] == '[' || s[i] 阅读全文
posted @ 2020-10-03 06:28 CasperWin 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 题解 Medium Tree + BFS class Solution { public: vector<int> rightSideView(TreeNode* root) { if(!root) return {}; vector<int> ret; queue<TreeNode*> q; q. 阅读全文
posted @ 2020-10-03 05:01 CasperWin 阅读(46) 评论(0) 推荐(0) 编辑
摘要: 题解 Medium BFS 这道题有点类似Number of Islands,可以用DFS或者BFS,但我觉得要更难一点,因为涉及到更新记忆数组。下面我的做法是用BFS(今天主要是想要练习BFS的套路)。 看了官方给出的答案,我的做法还是有几点值得改进的,其中一点是避免使用了记忆数组。因为从根本上来 阅读全文
posted @ 2020-10-03 04:35 CasperWin 阅读(58) 评论(0) 推荐(0) 编辑
摘要: 题解 Medium 用BFS再做了一遍。 class Solution { public: int numIslands(vector<vector<char>>& grid) { if(grid.empty()) return 0; int count = 0; vector<vector<boo 阅读全文
posted @ 2020-10-03 03:44 CasperWin 阅读(88) 评论(0) 推荐(0) 编辑
摘要: 题解 Easy class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* prehead = new ListNode(0); ListNode* p = prehead; whi 阅读全文
posted @ 2020-10-03 03:00 CasperWin 阅读(56) 评论(0) 推荐(0) 编辑
摘要: 题解 Medium BFS 一个节点只计算一次,这一点要注意。 class Solution { public: bool validTree(int n, vector<vector<int>>& edges) { vector<unordered_set<int>> graph(n, unord 阅读全文
posted @ 2020-10-02 14:17 CasperWin 阅读(58) 评论(0) 推荐(0) 编辑
摘要: 题解 Easy | BFS 典型的求最大深度,用BFS。 class Solution { public: int maxDepth(Node* root) { if(!root) return 0; queue<Node*> q; q.push(root); int max_depth = 0; 阅读全文
posted @ 2020-10-02 13:37 CasperWin 阅读(75) 评论(0) 推荐(0) 编辑
摘要: 题解 Medium BFS Deep Copy 的一个例子。 class Solution { public: Node* cloneGraph(Node* node) { if(!node) return nullptr; unordered_map<Node*, Node*> m; Node* 阅读全文
posted @ 2020-10-02 13:22 CasperWin 阅读(64) 评论(0) 推荐(0) 编辑
摘要: 题解 Hard 难死了。 class Solution { public: vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) { unordered_set<s 阅读全文
posted @ 2020-10-02 12:55 CasperWin 阅读(62) 评论(0) 推荐(0) 编辑
摘要: 题解 Hard BFS class Solution { public: void cleanRoom(Robot& robot) { dfs(robot, 0, 0, 0); } void goback(Robot &r) { r.turnLeft(); r.turnLeft(); r.move( 阅读全文
posted @ 2020-10-02 11:37 CasperWin 阅读(104) 评论(0) 推荐(0) 编辑
摘要: 题解 Medium 方法:BFS class Solution { public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { int steps = 0; unordered_set< 阅读全文
posted @ 2020-10-02 09:24 CasperWin 阅读(85) 评论(0) 推荐(0) 编辑
摘要: 题解 Easy 我倒是觉得不太容易,即使看了答案,也不确定这是令人信服的解法。直到看了动态图的讲解,所以还是画图更直观。 class Solution { public: int maxDistance(vector<vector<int>>& arrays) { int res = 0, min_ 阅读全文
posted @ 2020-10-02 07:53 CasperWin 阅读(125) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 ··· 17 下一页