摘要: 222.完全二叉树的节点个数[https://leetcode.cn/problems/count-complete-tree-nodes/submissions/498293461/] 思路:递归法 class Solution { public int maxDepth(TreeNode roo 阅读全文
posted @ 2024-01-25 17:49 糟糕的家伙 阅读(10) 评论(0) 推荐(0)
摘要: 226.翻转二叉树[https://leetcode.cn/problems/invert-binary-tree/description/] 递归:递归三部曲:①确定递归函数的参数和返回值 ②确定终止条件 ③确定单层递归的逻辑 /** * Definition for a binary tree 阅读全文
posted @ 2024-01-24 20:54 糟糕的家伙 阅读(10) 评论(0) 推荐(0)
摘要: 144.二叉树的前序遍历[https://leetcode.cn/problems/binary-tree-preorder-traversal/] 思路:栈实现的迭代遍历:出栈记录,右孩子非空右孩子进栈,左孩子非空左孩子进栈。 /** * Definition for a binary tree 阅读全文
posted @ 2024-01-23 19:14 糟糕的家伙 阅读(11) 评论(0) 推荐(0)
摘要: 239. 滑动窗口最大值[https://leetcode.cn/problems/sliding-window-maximum/submissions/497438333/] 思路:滑动窗口大小为K,现在前K个数中找到Max值进入ans数组,然后开始向后遍历,每进入一个数字时先判断if(nums[ 阅读全文
posted @ 2024-01-22 18:36 糟糕的家伙 阅读(14) 评论(0) 推荐(0)
摘要: 20.有效的括号[https://leetcode.cn/problems/valid-parentheses/] 思路:使用栈存放字符元素,遇到左括号就进栈,遇到右括号就弹出栈顶元素判断是否匹配。注意其实判断字符串长度是否为偶数进行剪枝,并且结束时判断栈是否为空。 class Solution { 阅读全文
posted @ 2024-01-20 16:42 糟糕的家伙 阅读(11) 评论(0) 推荐(0)
摘要: 232.栈实现队列[https://leetcode.cn/problems/implement-queue-using-stacks/description/] 思路:无 class MyQueue { Stack<Integer> stackIn; Stack<Integer> stackOut 阅读全文
posted @ 2024-01-19 17:30 糟糕的家伙 阅读(6) 评论(0) 推荐(0)
摘要: 28. 找出字符串中第一个匹配项的下标[https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/description/] 思路:KMP算法,重点在于求NEXT数组。还不能理解..暂时先背下来了。 阅读全文
posted @ 2024-01-19 17:27 糟糕的家伙 阅读(8) 评论(0) 推荐(0)
摘要: 344.翻转字符串[https://leetcode.cn/problems/reverse-string/submissions/496111203/] 思路:类似于原地翻转数组,左指针右指针向中间靠拢,交换对应元素。 class Solution { public void reverseStr 阅读全文
posted @ 2024-01-17 18:48 糟糕的家伙 阅读(12) 评论(0) 推荐(0)
摘要: 383.赎金信https://leetcode.cn/problems/ransom-note/description/ 思路:本题与242.有效的字母异位词几乎相同。将字母-'a',变成0-26的数字存放于数组中,再遍历数组对比次数。 class Solution { public boolean 阅读全文
posted @ 2024-01-16 17:20 糟糕的家伙 阅读(16) 评论(0) 推荐(0)
摘要: 242.有效的字母异位词https://leetcode.cn/problems/valid-anagram/description/ 思路:做一个基础版哈希表,哈希函数为key-'a',这样两个字符串的每个字母都会映射在26长的数组中,使用数组自增记录字母出现次数。 class Solution 阅读全文
posted @ 2024-01-15 15:48 糟糕的家伙 阅读(8) 评论(0) 推荐(0)