240
笔下虽有千言,胸中实无一策
摘要: 题解 双指针 三次遍历,但不影响复杂度为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) 编辑