240
笔下虽有千言,胸中实无一策
上一页 1 2 3 4 5 6 7 8 9 ··· 17 下一页
摘要: ##题解 很简单。 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 阅读(60) 评论(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 阅读(74) 评论(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 阅读(88) 评论(0) 推荐(0) 编辑
摘要: 题解 方法一:哈希表 一道很老的题,之前是用双指针的方法做的。不过已经隔了很久,今天再拿到的时候,受之前TwoSum的影响,思考路线一直被"哈希表"占据。完全按照 Leetcode 3. TwoSum 的做法的话会有许多重复的结果。需要想办法去重。 第一个去重是结果去重,这是保证结果正确。可以用一个 阅读全文
posted @ 2020-09-18 08:10 CasperWin 阅读(140) 评论(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 阅读(98) 评论(0) 推荐(0) 编辑
摘要: 题解 双指针法 用一个哈希表或者set记录当前子字符串中出现的字符,一旦有重复的字符进来,就去掉首字母,知道没有重复。 class Solution { public: int lengthOfLongestSubstring(string s) { int max_len = 0; set<cha 阅读全文
posted @ 2020-09-18 06:24 CasperWin 阅读(72) 评论(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 阅读(79) 评论(0) 推荐(0) 编辑
摘要: 题解 这道题做了若干遍,推荐的做法是drown every island的做法,也就是用初始的grid矩阵同时作为记忆矩阵,那么可以省去一个visited矩阵。这里作为DFS练习,还是套用visited矩阵的做法。 class Solution { public: int numIslands(ve 阅读全文
posted @ 2020-09-14 14:35 CasperWin 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 题解 方法:DFS Search 找两个节点的最低祖宗节点,想到用深度优先搜索,先找到从根节点到达该节点的路径,然后遍历两条路径,找到最低的共同祖宗节点。可以通过测试。 然后后来发现我把问题复杂化了,忽略了题目中的条件,“平衡二叉树”,也就是说,节点是有顺序的,而我完全没有用上。 class Sol 阅读全文
posted @ 2020-09-11 03:19 CasperWin 阅读(95) 评论(0) 推荐(0) 编辑
摘要: 题解 事实证明,这种有着详实背景介绍的题干,大部分与算法本身无关,反而是直接看sample input和output更容易理解题目要求。 同时,也证明,用sample进行手动debug很有效。 一旦意思理解了,题目似乎并不难。也就是根据0/1移动至当前节点的左右节点,一旦遇到叶子节点,便把对应数据更 阅读全文
posted @ 2020-09-11 02:27 CasperWin 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 题解 基于 Reverse Linked List 的同样思路,稍加修改,即可。但代码不够简洁,看上去过程也有些冗余。 class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode 阅读全文
posted @ 2020-09-09 04:09 CasperWin 阅读(94) 评论(0) 推荐(0) 编辑
摘要: 题解 很基础的题,与reverseDoublyLinkedList(Hackerrank)那道题作对照,实际上只有一样代码的差别。 class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head-> 阅读全文
posted @ 2020-09-09 03:21 CasperWin 阅读(131) 评论(0) 推荐(0) 编辑
摘要: 题解 与Leetcode上翻转链表(206. Reverse Linked List)思路一致,只不过多了一个“prev”前节点的处理,这种题通过画图比较容易验证。 DoublyLinkedListNode* reverse(DoublyLinkedListNode* head) { if(!hea 阅读全文
posted @ 2020-09-09 03:17 CasperWin 阅读(119) 评论(0) 推荐(0) 编辑
摘要: 题解 很简单的题,要注意检查空节点。 DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) { if(!head) { head = new DoublyLinkedListNode(data); retur 阅读全文
posted @ 2020-09-09 02:26 CasperWin 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 题解 方法一:龟兔赛跑 最先想到的方法,姑且叫它“龟兔赛跑”:先计算两条LinkedList的长度,然后让长的那条先走,直到剩下的节点数一样,相当于回到同一起跑线。接下来,以同样的步调向后遍历并比较节点。代码写起来不是很简洁,但是并不影响复杂度,能通过。 class Solution { publi 阅读全文
posted @ 2020-09-09 02:11 CasperWin 阅读(124) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 ··· 17 下一页