240
笔下虽有千言,胸中实无一策

随笔分类 -  刷题

Leetcode, Lintcode
摘要:题解 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 阅读(94) 评论(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 阅读(68) 评论(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 阅读(87) 评论(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 阅读(78) 评论(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 阅读(66) 评论(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 阅读(110) 评论(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 阅读(98) 评论(0) 推荐(0)
摘要:题解 Easy 我倒是觉得不太容易,即使看了答案,也不确定这是令人信服的解法。直到看了动态图的讲解,所以还是画图更直观。 class Solution { public: int maxDistance(vector<vector<int>>& arrays) { int res = 0, min_ 阅读全文
posted @ 2020-10-02 07:53 CasperWin 阅读(136) 评论(0) 推荐(0)
摘要:题解 Hard 方法:Trie struct TrieNode { vector<int> indexes; vector<TrieNode*> next; TrieNode() : next(26, nullptr) { } }; class Solution { public: vector<v 阅读全文
posted @ 2020-10-01 07:47 CasperWin 阅读(103) 评论(0) 推荐(0)
摘要:题解 Medium Heap (Priority Queue) 难点是自己写comparator。 class comparator { public: bool operator() (const pair<string, int>& a, const pair<string, int>& b) 阅读全文
posted @ 2020-10-01 06:50 CasperWin 阅读(96) 评论(0) 推荐(0)
摘要:题解 Medium Topological Sort class Solution { public: vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) { vector<vector<int>> gr 阅读全文
posted @ 2020-10-01 05:52 CasperWin 阅读(89) 评论(0) 推荐(0)
摘要:题解 Medium 方法:Topological Sort class Solution { public: bool canFinish(int numCourses, vector<vector<int>>& prerequisites) { vector<vector<int>> graph( 阅读全文
posted @ 2020-10-01 05:42 CasperWin 阅读(92) 评论(0) 推荐(0)
摘要:题解 Easy | DFS class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> paths; dfs(root, "", paths); return paths; } vo 阅读全文
posted @ 2020-09-30 14:53 CasperWin 阅读(76) 评论(0) 推荐(0)
摘要:题解 Medium | DFS class Solution { public: vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int>> paths; vector<int> path; dfs(root, 阅读全文
posted @ 2020-09-30 14:44 CasperWin 阅读(68) 评论(0) 推荐(0)
摘要:题解 Hard 方法一:DFS + Memoization class Solution { public: int longestIncreasingPath(vector<vector<int>>& matrix) { if(matrix.empty()) return 0; m = matri 阅读全文
posted @ 2020-09-30 14:27 CasperWin 阅读(89) 评论(0) 推荐(0)
摘要:题解 Easy 方法一:Greedy 贪心算法 class Solution { public: string longestWord(vector<string>& words) { string res; unordered_set<string> s; sort(words.begin(), 阅读全文
posted @ 2020-09-30 13:26 CasperWin 阅读(80) 评论(0) 推荐(0)
摘要:题解 Easy | Tree, DFS class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(!p && !q) return true; if(!p || !q) return false; if(p->va 阅读全文
posted @ 2020-09-30 12:25 CasperWin 阅读(78) 评论(0) 推荐(0)
摘要:题解 Easy | Tree, DFS class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(!root->left && !root->right) return 阅读全文
posted @ 2020-09-30 12:04 CasperWin 阅读(60) 评论(0) 推荐(0)
摘要:题解 Easy | Hashmap class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { unordered_map<int, int> m; for(int i = 0; i < number 阅读全文
posted @ 2020-09-29 13:31 CasperWin 阅读(55) 评论(0) 推荐(0)
摘要:题解 Medium | Backtracking class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vecto 阅读全文
posted @ 2020-09-29 12:58 CasperWin 阅读(101) 评论(0) 推荐(0)