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

09 2020 档案

摘要:题解 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 阅读(75) 评论(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 阅读(67) 评论(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 阅读(88) 评论(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 阅读(54) 评论(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 阅读(100) 评论(0) 推荐(0)
摘要:题解 Medium | Backtracking class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> 阅读全文
posted @ 2020-09-29 12:55 CasperWin 阅读(80) 评论(0) 推荐(0)
摘要:题解 Medium | Backtracking class Solution { public: int getMaximumGold(vector<vector<int>>& grid) { int ret = 0; for(int i = 0; i < grid.size(); i++) { 阅读全文
posted @ 2020-09-29 12:37 CasperWin 阅读(86) 评论(0) 推荐(0)
摘要:题解 Medium | Backtracking class Solution { public: vector<vector<int>> combinationSum3(int k, int n) { vector<vector<int>> sols; vector<int> sol; helpe 阅读全文
posted @ 2020-09-29 12:15 CasperWin 阅读(93) 评论(0) 推荐(0)
摘要:题解 Medium | Backtracking class Solution { public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { sort(candidates.begin(), 阅读全文
posted @ 2020-09-29 12:08 CasperWin 阅读(81) 评论(0) 推荐(0)
摘要:题解 Medium | Backtracking class Solution { public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { vector<vector<int>> sols; 阅读全文
posted @ 2020-09-29 12:00 CasperWin 阅读(73) 评论(0) 推荐(0)
摘要:题解 Medium | Backtracking 经典的排列组合题,使用回溯法。必须熟练。 class Solution { public: vector<vector<int>> combine(int n, int k) { vector<vector<int>> combinations; v 阅读全文
posted @ 2020-09-29 11:49 CasperWin 阅读(73) 评论(0) 推荐(0)
摘要:题解 Easy | Backtracking/DFS 这道题被标记为Easy级别,可是我感觉并不简单,还是花了一些时间的。 class Solution { public: vector<string> readBinaryWatch(int num) { set<string> ret; vect 阅读全文
posted @ 2020-09-25 09:49 CasperWin 阅读(79) 评论(0) 推荐(0)
摘要:题解 Hard | Backtracking/DFS 这道题使用标准的Backtracking套路,不难得解。一开始没有注意两点,导致错误: 起点不一定是在左上角的grid[0][0],而是需要根据条件找出来; 每一个非障碍位置都需要走一次,这就不同于之前的 Unique Paths 的题,有些格子 阅读全文
posted @ 2020-09-25 08:44 CasperWin 阅读(84) 评论(0) 推荐(0)
摘要:题解 Medium 动态规划 class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { if(obstacleGrid.empty()) return 0; int m = o 阅读全文
posted @ 2020-09-25 03:22 CasperWin 阅读(65) 评论(0) 推荐(0)
摘要:题解 Easy. 动态规划。 class Solution { public: int uniquePaths(int m, int n) { vector<vector<int>> dp(m, vector<int>(n)); for(int i = 0; i < m; i++) dp[i][0] 阅读全文
posted @ 2020-09-25 03:13 CasperWin 阅读(63) 评论(0) 推荐(0)
摘要:题解 Easy 动态规划 class Solution { public: int minCostClimbingStairs(vector<int>& cost) { if(cost.empty()) return 0; if(cost.size() == 1) return 0; if(cost 阅读全文
posted @ 2020-09-24 07:27 CasperWin 阅读(79) 评论(0) 推荐(0)
摘要:动态规划 Easy 动态规划 class Solution { public: int climbStairs(int n) { if(n == 1) return 1; vector<int> dp(n+1); dp[0] = 0; dp[1] = 1; dp[2] = 2; for(int i 阅读全文
posted @ 2020-09-24 06:51 CasperWin 阅读(71) 评论(0) 推荐(0)
摘要:题解 Medium 动态规划 将环形数组分解两个数组去处理,即[0:n-2]和[1:n-1]。最后取结果的较大值。 class Solution { public: int rob(vector<int>& nums) { if(nums.empty()) return 0; if(nums.siz 阅读全文
posted @ 2020-09-24 06:44 CasperWin 阅读(87) 评论(0) 推荐(0)
摘要:题解 动态规划 Easy class Solution { public: int rob(vector<int>& nums) { if(nums.empty()) return 0; if(nums.size() == 1) return nums[0]; if(nums.size() == 2 阅读全文
posted @ 2020-09-24 06:20 CasperWin 阅读(83) 评论(0) 推荐(0)
摘要:题解 动态规划。 Easy class Solution { public: int numWays(int n, int k) { if(n == 0) return 0; if(n == 1) return k; int num = 0; // dp1[i]: the number of way 阅读全文
posted @ 2020-09-24 05:59 CasperWin 阅读(127) 评论(0) 推荐(0)
摘要:题解 动态规划,Hard级别。 Paint House 的升级版,如果颜色数量不是三种,而是若干种。直接套用256的思路,问题得解。 class Solution { public: int minCostII(vector<vector<int>>& costs) { if(costs.empty 阅读全文
posted @ 2020-09-24 03:08 CasperWin 阅读(76) 评论(0) 推荐(0)
摘要:题解 动态规划,Easy级别。 动态规划题,重点是找到传递公式,思路见注释。 class Solution { public: int minCost(vector<vector<int>>& costs) { if(costs.empty()) return 0; int n = costs.si 阅读全文
posted @ 2020-09-24 01:37 CasperWin 阅读(57) 评论(0) 推荐(0)
摘要:题解 双指针法,移动窗口法(Sliding Window) 很重要的题。 这道题要求地很细,移动窗口的时候,要同时记录,开始位置、长度和包含目标字符的个数count。 class Solution { public: string minWindow(string s, string t) { in 阅读全文
posted @ 2020-09-20 10:04 CasperWin 阅读(106) 评论(0) 推荐(0)
摘要:##题解 很简单。 class Solution { public: vector<int> plusOne(vector<int>& digits) { int carry = 1; for(int i = digits.size()-1; i >= 0; i--) { int temp = ca 阅读全文
posted @ 2020-09-20 09:02 CasperWin 阅读(61) 评论(0) 推荐(0)
摘要:题解 入门级的动态规划题。 class Solution { public: bool canJump(vector<int>& nums) { // dp[i] = dp[i-1] vector<int> dp(nums.size(), false); dp[0] = true; for(int 阅读全文
posted @ 2020-09-20 08:54 CasperWin 阅读(78) 评论(0) 推荐(0)
摘要:题解 双指针法 area = (j-i)*min(height[i], height[j]); class Solution { public: int maxArea(vector<int>& height) { int max_area = 0; int l = 0, r = height.si 阅读全文
posted @ 2020-09-20 08:00 CasperWin 阅读(93) 评论(0) 推荐(0)
摘要:题解 方法一:哈希表 一道很老的题,之前是用双指针的方法做的。不过已经隔了很久,今天再拿到的时候,受之前TwoSum的影响,思考路线一直被"哈希表"占据。完全按照 Leetcode 3. TwoSum 的做法的话会有许多重复的结果。需要想办法去重。 第一个去重是结果去重,这是保证结果正确。可以用一个 阅读全文
posted @ 2020-09-18 08:10 CasperWin 阅读(152) 评论(0) 推荐(0)
摘要:题解 方法:哈希表 class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> hash; for(int i = 0; i < nums.size(); i 阅读全文
posted @ 2020-09-18 06:36 CasperWin 阅读(104) 评论(0) 推荐(0)
摘要:题解 双指针法 用一个哈希表或者set记录当前子字符串中出现的字符,一旦有重复的字符进来,就去掉首字母,知道没有重复。 class Solution { public: int lengthOfLongestSubstring(string s) { int max_len = 0; set<cha 阅读全文
posted @ 2020-09-18 06:24 CasperWin 阅读(80) 评论(0) 推荐(0)
摘要:题解 一道 hard 级别题。 递归 + 暴力查找前缀,超时了。 class Solution { public: vector<vector<string>> wordSquares(vector<string>& words) { // the size of a word determine 阅读全文
posted @ 2020-09-16 06:06 CasperWin 阅读(85) 评论(0) 推荐(0)
摘要:题解 这道题做了若干遍,推荐的做法是drown every island的做法,也就是用初始的grid矩阵同时作为记忆矩阵,那么可以省去一个visited矩阵。这里作为DFS练习,还是套用visited矩阵的做法。 class Solution { public: int numIslands(ve 阅读全文
posted @ 2020-09-14 14:35 CasperWin 阅读(130) 评论(0) 推荐(0)
摘要:题解 方法:DFS Search 找两个节点的最低祖宗节点,想到用深度优先搜索,先找到从根节点到达该节点的路径,然后遍历两条路径,找到最低的共同祖宗节点。可以通过测试。 然后后来发现我把问题复杂化了,忽略了题目中的条件,“平衡二叉树”,也就是说,节点是有顺序的,而我完全没有用上。 class Sol 阅读全文
posted @ 2020-09-11 03:19 CasperWin 阅读(106) 评论(0) 推荐(0)
摘要:题解 事实证明,这种有着详实背景介绍的题干,大部分与算法本身无关,反而是直接看sample input和output更容易理解题目要求。 同时,也证明,用sample进行手动debug很有效。 一旦意思理解了,题目似乎并不难。也就是根据0/1移动至当前节点的左右节点,一旦遇到叶子节点,便把对应数据更 阅读全文
posted @ 2020-09-11 02:27 CasperWin 阅读(151) 评论(0) 推荐(0)
摘要:题解 基于 Reverse Linked List 的同样思路,稍加修改,即可。但代码不够简洁,看上去过程也有些冗余。 class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode 阅读全文
posted @ 2020-09-09 04:09 CasperWin 阅读(108) 评论(0) 推荐(0)
摘要:题解 很基础的题,与reverseDoublyLinkedList(Hackerrank)那道题作对照,实际上只有一样代码的差别。 class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head-> 阅读全文
posted @ 2020-09-09 03:21 CasperWin 阅读(140) 评论(0) 推荐(0)
摘要:题解 与Leetcode上翻转链表(206. Reverse Linked List)思路一致,只不过多了一个“prev”前节点的处理,这种题通过画图比较容易验证。 DoublyLinkedListNode* reverse(DoublyLinkedListNode* head) { if(!hea 阅读全文
posted @ 2020-09-09 03:17 CasperWin 阅读(133) 评论(0) 推荐(0)
摘要:题解 很简单的题,要注意检查空节点。 DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) { if(!head) { head = new DoublyLinkedListNode(data); retur 阅读全文
posted @ 2020-09-09 02:26 CasperWin 阅读(143) 评论(0) 推荐(0)
摘要:题解 方法一:龟兔赛跑 最先想到的方法,姑且叫它“龟兔赛跑”:先计算两条LinkedList的长度,然后让长的那条先走,直到剩下的节点数一样,相当于回到同一起跑线。接下来,以同样的步调向后遍历并比较节点。代码写起来不是很简洁,但是并不影响复杂度,能通过。 class Solution { publi 阅读全文
posted @ 2020-09-09 02:11 CasperWin 阅读(136) 评论(0) 推荐(0)