算法题型练习
前言
归纳一些常见数据结构对应算法题,可以针对性练习。
不同题型
数组与链表
1、844题,比较含退格的字符串:
https://leetcode-cn.com/problems/backspace-string-compare/
2、846题,两两交换链表中的节点:
https://leetcode-cn.com/problems/swap-nodes-in-pairs/
3、847题,环形链表:
https://leetcode-cn.com/problems/linked-list-cycle/
4、15题,三数之和:
https://leetcode-cn.com/problems/3sum/
5、169题,求众数:
https://leetcode.cn/problems/majority-element/
6、41题,缺失的第一个正数:
https://leetcode-cn.com/problems/first-missing-positive/
7、141题,环形链表:
https://leetcode-cn.com/problems/linked-list-cycle-ii/
8、23题,合并K个排序链表:
https://leetcode-cn.com/problems/merge-k-sorted-lists/
一些小结:一般数组和链表题,考虑数据是否经过一轮处理,处理时使用什么数据结构更好;在链表题中考虑是否快慢指针、双指针进行实现。
栈与队列
1、20题,有效的括号:
https://leetcode-cn.com/problems/valid-parentheses/
2、232题,用栈实现队列:
https://leetcode-cn.com/problems/implement-queue-using-stacks/
3、225题,用队列实现栈:
https://leetcode-cn.com/problems/implement-stack-using-queues/
优先队列
1、703题,数据流中的第 K 大元素:
https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/
2、239,滑动窗口最大值:
https://leetcode-cn.com/problems/sliding-window-maximum/
3、Offer41题,中位数计算:https://leetcode-cn.com/problems/shu-ju-liu-zhong-de-zhong-wei-shu-lcof/
哈希表
1、242题,有效的字母异位词:
https://leetcode-cn.com/problems/valid-anagram/
2、1题,两数之和:
https://leetcode-cn.com/problems/two-sum/
树&二叉树&二叉搜索树
1、235题,二叉搜索树的最近公共祖先:
https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
2、236题,二叉树的最近公共祖先:
https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/
3、Offer55题,二叉树的深度:
https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/ 🌟
二叉树遍历 & 递归与分治
1、50题,Pow(x, n):
https://leetcode-cn.com/problems/powx-n/
2、169题,多数元素:
https://leetcode-cn.com/problems/majority-element/
3、Offer 07题,重建二叉树:
https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/submissions/ 🌟
贪心
1、122题,买卖股票的最佳时机 II:
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
广度优先与深度优先
1、102题,二叉树的层序遍历:
https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
2、104题,二叉树的最大深度:
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
3、111题,二叉树的最小深度:
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
4、22题,括号生成:
https://leetcode-cn.com/problems/generate-parentheses/ 🌟
抽象能递归的题(DFS/BFS),需要有“上下层”概念,从上层往下层依次找
回溯(递归):从上往下推
对比DP,动态规划(递推):与递归反过来,从下往上推,存在最优子结构
剪枝
1、51题,N皇后:
https://leetcode.cn/problems/n-queens/
2、36题,有效的数独:
https://leetcode-cn.com/problems/valid-sudoku/
37、解数独(36题的升级版):
https://leetcode.cn/problems/sudoku-solver/
二分查找
left小于等于figh;求mid越界问题;left和right的更新问题最最重要
1、69题Sqrt(x):
https://leetcode-cn.com/problems/sqrtx/ 🌟🌟
2、Offer11题,旋转数组的最小数字:
https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/submissions/ 🌟🌟
字典数
空间换时间思想,利用字符串的公共前缀来降低查询的开销以达到提高效率的目的
1、208题,实现 Trie (前缀树):
https://leetcode-cn.com/problems/implement-trie-prefix-tree/ 🌟
2、79题,单词搜索:
https://leetcode-cn.com/problems/word-search/
位运算(2进制)
1、191题,位1的个数:
https://leetcode-cn.com/problems/number-of-1-bits/ 🌟
2、231题,2 的幂:
https://leetcode-cn.com/problems/power-of-two/
3、338题,比特位计数:
https://leetcode-cn.com/problems/counting-bits/
动态规划
反方向思考,子结构
1、70题,爬楼梯:
https://leetcode-cn.com/problems/climbing-stairs/
2、120题,三角形最小路径和:
https://leetcode-cn.com/problems/triangle/
3、152题,乘积最大子数组:
https://leetcode-cn.com/problems/maximum-product-subarray/
4、121题,买卖股票的最佳时机:
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
5、300题,最长递增子序列:
https://leetcode-cn.com/problems/longest-increasing-subsequence/
6、322题,零钱兑换:
https://leetcode-cn.com/problems/coin-change/ 🌟🌟
双指针
1、Offer52题,两个链表的第一个公共节点:
https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/submissions/ 🌟🌟
LRU
https://leetcode.cn/problems/lru-cache/description/
总结
多练,多总结!