合集-代码随想录

摘要:## 题目:[704. 二分查找](https://leetcode.cn/problems/binary-search/) ### 思路: 二分查找一般是在**有序的**数组中查找指定的值,单纯的查找值,把数组跑一遍的复杂度为O(n)。 二分查找每次把范围缩小一半,我们每次都去中间的值,有以下三种 阅读全文
posted @ 2023-07-26 22:05 WtcSky 阅读(147) 评论(0) 推荐(0) 编辑
摘要:## 题目:[977. 有序数组的平方](https://leetcode.cn/problems/squares-of-a-sorted-array/) ### 思路: 一开始的思路是从中间向两边扩: 1. 找到第一个大于等于0的位置r;判断nums[r]是否大于等于0,如果不是赋值为`len(n 阅读全文
posted @ 2023-07-27 22:26 WtcSky 阅读(7) 评论(0) 推荐(0) 编辑
摘要:## 题目:[454. 四数相加 II](https://leetcode.cn/problems/4sum-ii/) ### 思路: 首先,因为下标不同,因此相同的序列可能会出现很多次。 A + B + C + D = 0,那么当知道保存了A+B的和之后,就看有没有A + B = 0 - C - 阅读全文
posted @ 2023-08-01 21:50 WtcSky 阅读(5) 评论(0) 推荐(0) 编辑
摘要:## 题目:[242. 有效的字母异位词](https://leetcode.cn/problems/valid-anagram/) ### 思路: 很简单,就是看两个字符串每个字母出现的次数是不是相同的。 可以用两个数组来比较,也可以用一个数组比较。 ### 代码: 一个数组 ```go func 阅读全文
posted @ 2023-07-31 21:00 WtcSky 阅读(6) 评论(0) 推荐(0) 编辑
摘要:## 题目:[24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/) ### 思路: ![6](https://s2.loli.net/2023/07/29/haIsMPevD7OfEpK.png) 首先给他加一个虚拟头结 阅读全文
posted @ 2023-07-29 21:32 WtcSky 阅读(7) 评论(0) 推荐(0) 编辑
摘要:## 题目:[203. 移除链表节点](https://leetcode.cn/problems/remove-linked-list-elements/) ### 思路: ![20210316095619221](https://s2.loli.net/2023/07/28/s47u8vofDKy 阅读全文
posted @ 2023-07-28 22:12 WtcSky 阅读(6) 评论(0) 推荐(0) 编辑
摘要:## 题目:[104. 二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/) ### 思路: 本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。 - 二叉树节点的深 阅读全文
posted @ 2023-08-10 21:57 WtcSky 阅读(8) 评论(0) 推荐(0) 编辑
摘要:## 题目:[102. 二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/) ### 思路: 先把根放进去,然后每次都是左右就可以了。 记录一个深度,当`len(res) == deepth`的时候就说明 阅读全文
posted @ 2023-08-09 20:36 WtcSky 阅读(8) 评论(0) 推荐(0) 编辑
摘要:今天的题目就是二叉树的前中后序遍历,目前只写了递归方法,之后再补迭代方法。 ## 题目:[144. 二叉树的前序遍历](https://leetcode.cn/problems/binary-tree-preorder-traversal/) ### 思路: 前序遍历:根-左-右 ### 代码1: 阅读全文
posted @ 2023-08-08 21:10 WtcSky 阅读(13) 评论(0) 推荐(0) 编辑
摘要:## 题目:[239. 滑动窗口最大值](https://leetcode.cn/problems/sliding-window-maximum/) ### 思路: ![239.滑动窗口最大值.gif](https://s2.loli.net/2023/08/07/9ohI1LGQlZmMVjn.g 阅读全文
posted @ 2023-08-07 21:30 WtcSky 阅读(9) 评论(0) 推荐(0) 编辑
摘要:## 题目:[20. 有效的括号](https://leetcode.cn/problems/valid-parentheses/) ### 思路: 很简单的一个栈的题目: 1. 如果是左括号就存 2. 如果是右括号就和栈顶的匹配 1. 匹配失败就返回false 2. 匹配成功就删除栈顶元素 3. 阅读全文
posted @ 2023-08-05 12:44 WtcSky 阅读(10) 评论(0) 推荐(0) 编辑
摘要:## 题目:[232. 用栈实现队列](https://leetcode.cn/problems/implement-queue-using-stacks/) ### 思路: 因为go没有栈和队列的类型,直接自己写就行了。 比较简单的实现,具体看代码中的注释。 ### 代码: ```go type 阅读全文
posted @ 2023-08-04 21:10 WtcSky 阅读(7) 评论(0) 推荐(0) 编辑
摘要:## 题目:[28. 找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/) ### 思路: 说白了就是匹配字符串,朴素就是暴力以每一个位置为起点都跑一遍。 阅读全文
posted @ 2023-08-03 20:58 WtcSky 阅读(9) 评论(0) 推荐(0) 编辑
摘要:## 题目:[344. 反转字符串](https://leetcode.cn/problems/reverse-string/) ### 思路: 每次把最前面和最后面的交换位置即可 `strings`库里没有反转的方法 ——这个反转是之后几个题的一个基础 ### 代码: 双指针调换位置 ```go 阅读全文
posted @ 2023-08-02 23:15 WtcSky 阅读(9) 评论(0) 推荐(0) 编辑
摘要:## 题目:[860. 柠檬水找零](https://leetcode.cn/problems/lemonade-change/) ### 思路: 收到钱三种情况: 1. 5刀:直接收起来就可以了,不需要找钱 2. 10刀:收到10刀,需要找5刀,如果没有5刀,就返回false,否则5刀-1 3. 阅读全文
posted @ 2023-08-29 11:43 WtcSky 阅读(5) 评论(0) 推荐(0) 编辑
摘要:## 题目:[1005.K次取反后最大化的数组和](https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/) ### 思路: 思路是: 1. 先把负数从小到大变成正数(即绝对值由大到小) 2. 如果还需要变化(k>0 阅读全文
posted @ 2023-08-28 16:20 WtcSky 阅读(6) 评论(0) 推荐(0) 编辑
摘要:## 题目:[122. 买卖股票的最佳时机 II](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/) ### 思路: 假如第 0 天买入,第 3 天卖出,那么利润为:prices[3] - prices[0]。 相当于 阅读全文
posted @ 2023-08-27 01:14 WtcSky 阅读(5) 评论(0) 推荐(0) 编辑
摘要:## 题目:[455. 分发饼干](https://leetcode.cn/problems/assign-cookies/ "455. 分发饼干") ### 思路: 贪心,思路是尽量先给胃口值小的分,饼干也是从小的开始分: 1. 如果饼干满足了胃口值,结果+1换下一个人,下一个饼干 2. 如果饼干 阅读全文
posted @ 2023-08-25 14:37 WtcSky 阅读(8) 评论(0) 推荐(0) 编辑
摘要:## 题目:[332. 重新安排行程](https://leetcode.cn/problems/reconstruct-itinerary/) ### 思路: 其实这里已经是图的部分了,回溯应该也可以。Hierholzer算法解决欧拉问题 ### 代码: ```go func findItiner 阅读全文
posted @ 2023-08-24 10:29 WtcSky 阅读(9) 评论(0) 推荐(0) 编辑
摘要:## 题目:[491. 递增子序列](https://leetcode.cn/problems/non-decreasing-subsequences/ "491. 递增子序列") ### 思路: 核心问题——同层去重,这一题不能够重新排序因此不可以用`i > index && nums[i] == 阅读全文
posted @ 2023-08-23 11:10 WtcSky 阅读(5) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示