基础练习题目
背景
做编程题目需要的基础,分为如下两块:
- 排序(几种基本的排序算法、链表排序)
- 二分搜索
- 二叉树的深度优先遍历和广度优先遍历
- 深度优先遍历的三种方式(递归、非递归写法)
- 广度优先遍历 (递归、非递归写法)
二叉树
树的遍历递归、非递归写法。默写+练习
- 先序遍历
- https://leetcode.com/problems/binary-tree-preorder-traversal/
- 中序遍历
- https://leetcode.com/problems/binary-tree-inorder-traversal/
- 后序遍历
- https://leetcode.com/problems/binary-tree-postorder-traversal/
- 层次遍历
- https://leetcode.com/problems/binary-tree-level-order-traversal/
排序
- 快排的应用
- 寻找第k大数 堆的方法+双指针法
- https://leetcode.com/problems/kth-largest-element-in-an-array/submissions/
- 奇偶数互换(双指针法)
- https://leetcode.com/problems/sort-array-by-parity/
- 寻找第k大数 堆的方法+双指针法
- mergeSort
- 数组种的逆序对
- 牛客网
- 数组种的逆序对
双指针法
类型1:一个头指针,一个尾指针,不断缩小搜索空间
- 偶数排列在奇数之前
- https://leetcode.com/problems/sort-array-by-parity/
- 3sum 基础问题
- https://leetcode.com/problems/3sum/
- 3Sum Closest
- https://leetcode.com/problems/3sum-closest/
- 4 sum
- https://leetcode.com/problems/4sum/submissions/
- Container With Most Water
- https://leetcode.com/problems/container-with-most-water/
- Two Sum II - Input Array Is Sorted
- https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
类型2:快慢指针法
链表
- 链表反转 (双向链表反转)基础中的基础 核心思想:两个指针反转,一个指针指向保存之前的记忆(共三个指针)
- https://leetcode.com/problems/reverse-linked-list/
- 链表合并
- 两个链表合并
- https://leetcode.com/problems/merge-two-sorted-lists/submissions/
- k个链表合并
- https://leetcode.com/problems/merge-k-sorted-lists/submissions/
- 堆做法
- 复杂度 n*log(k)
- 创建堆的复杂度是k,取数的复杂度是log(k), 假设有n个数,构建堆的复杂度是n,每一次取数的复杂度是log(k)
- 复杂度 n*log(k)
- 使用队列,不断的合并两个list,直到最后只剩一个list为止
- 时间复杂度
- 总共n个节点,一共合并了log(k)次,总的时间复杂度是O(nlog(k))
- 时间复杂度
- 递归做法
- mergesrot
- 总共合并了logk次,一共n个节点进行了合并,复杂度还是O(nlog(k))
- 堆做法
- https://leetcode.com/problems/merge-k-sorted-lists/submissions/
- 链表模拟加减法
- Add Two Numbers 考察细心
- https://leetcode.com/problems/add-two-numbers/
- Add Two Numbers 考察细心
- 删除链表中第N个节点,如果N是第一个节点要注意
- https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/
- 链表排序
- https://leetcode.com/problems/sort-list/
- 使用mergeSort
- 核心思想:找到中间节点,而后将一个链表分裂成两个链表,而后进行merge
- 使用mergeSort
- https://leetcode.com/problems/sort-list/
- JZ86 在二叉树中找到两个节点的最近公共祖先
- 基本思路:
- 找到从根节点到目标阶段的一条路径,二叉树,所以只有一条路径
- 找到两条路径第一个相同的点
- 基本思路:
- 两个链表合并
动态规划
- 最长递增子序列
- 连接:https://leetcode.com/problems/longest-increasing-subsequence/
二分搜索
- 旋转数组的最小数字
- 牛客
- 二维数组中的查找
- 牛客
- 方法很多:
- 线性查找(一次减少一列)
- 一个二分查找
- 两个二分查找