[Algorithm] 算法训练 202604
Categories Algorithm
Tags
- 704. 二分查找
- 59. 螺旋矩阵II
- 44. 开发商购买土地
- 203. 移除链表元素
- 707. 设计链表
- 206. 反转链表
- 142. 环形链表II
- 242. 有效的字母异位词
- 349. 两个数组的交集
- 1. 两数之和 这题从没想到,返回的数组下标一定是最后面的
- 454. 四数相加II 如果是n数相加,时间复杂度是O(n / 2 上取整)
- 54. 替换数字(第八期模拟笔试)
- 151. 翻转字符串里的单词 想了老半天
- 28. 实现 strStr()
- 459.重复的子字符串
- 214. Shortest Palindrome
- 128. Longest Consecutive Sequence
- 15. 三数之和
- 18. 四数之和 去重!!!
- 232. 用栈实现队列
- 225. 用队列实现栈
- 239. 滑动窗口最大值
- 347. 前 K 个高频元素
- 94. Binary Tree Inorder Traversal
- 226. Invert Binary Tree 中序、前后序、层序、递归 四种方法都可以
- 101. 对称二叉树 递归和迭代
- 111. Minimum Depth of Binary Tree 最小深度要在叶子结点处理
- 222. Count Complete Tree Nodes 递归利用完全二叉树性质
- 110. Balanced Binary Tree 高度后序 深度前序
- 513. Find Bottom Left Tree Value 前序遍历 如果交换left和right呢?
- 112. Path Sum 迭代 pair
- 106. Construct Binary Tree from Inorder and Postorder Traversal 用index
- 654. Maximum Binary Tree index
- 617. Merge Two Binary Trees 类似于对称二叉树
- 700. Search in a Binary Search Tree 迭代
- 98. Validate Binary Search Tree 数组 递归 中序迭代 记住中序遍历
- 501. Find Mode in Binary Search Tree 中序 众数处理
- 236. Lowest Common Ancestor of a Binary Tree 回溯 后序遍历
- 235. Lowest Common Ancestor of a Binary Search Tree 第一个区间内就是 注意本身就是祖先的情况
- 701. Insert into a Binary Search Tree 递归 迭代
- 450. Delete Node in a BST 递归 五种情况
- 669. Trim a Binary Search Tree
- 108. Convert Sorted Array to Binary Search Tree 用index构造 构造类题目都有套路 递归
- 538. Convert BST to Greater Tree 反中序遍历
- 77. Combinations 组合 剪枝
- 40. Combination Sum II 去重 used和candidate i-1 i
- 39. Combination Sum 注意参数是i还是i+1还是start
- 216. Combination Sum III 好多边界…
- 131. Palindrome Partitioning
- 470. Implement Rand10() Using Rand7() (randX - 1) * Y + randY = randXY
- 78. Subsets 其实就是终止条件改改
start == size不是错误 - 46. Permutations used数组
nums.size() == p.size() - 491. Non-decreasing Subsequences 不能排序 不能用原来的去重 用set
- 63. Unique Paths II 注意初始化条件
- 343. Integer Break
- 416. Partition Equal Subset Sum 记住01背包公式 边界条件
- 494. Target Sum 边界条件
dp[j] += dp[j - nums[i]] - 200. Number of Islands dfs bfs
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
void bfs(vector<vector<char>>& grid, int i, int j, vector<vector<bool>>& visited) {
queue<pair<int, int>> q;
q.push({i, j});
visited[i][j] = true;
while (!q.empty()) {
pair<int , int> p = q.front(); q.pop();
auto curi = p.first, curj = p.second;
for (int k = 0; k < 4; k++) {
auto nexti = curi + dir[k][0], nextj = curj + dir[k][1];
if (nexti < 0 || nextj < 0 || nexti >= grid.size() || nextj >= grid[0].size() || grid[nexti][nextj] == '0' || visited[nexti][nextj]) continue;
visited[nexti][nextj] = true;
q.push({nexti, nextj});
}
}
}
void dfs(vector<vector<char>>& grid, int i, int j, vector<vector<bool>> &visited) {
visited[i][j] = true;
for (int k = 0; k < 4; k++) {
auto nexti = i + dir[k][0], nextj = j + dir[k][1];
if (nexti < 0 || nexti >= grid.size() || nextj < 0 || nextj >= grid[0].size() || visited[nexti][nextj] || grid[nexti][nextj] != '1') {
continue;
}
dfs(grid, nexti, nextj, visited);
}
}
- 994. Rotting Oranges bfs
- 207. Course Schedule 邻接表 入度
- 3741. 三个相等元素之间的最小距离 II 哈希表记录相同的值的index
- 2463. 最小移动总距离 初始化和递推都使我大脑旋转
- 2515. 到目标字符串的最短距离 取模 加上
size()$$d = \min(|i - j|, n - |i - j|)$$ - 3488. 距离最小相等元素查询 和上一题和之前的题目很像,哈基表+二分
- 3761. 镜像对之间最小绝对距离 两数之和改版,注意reverse实现,
m[r] = i; r = reverse(x)表示为nums[i] == x - 1855. 下标对中的最大距离 维护窗口最大值 也可以二分
- 2078. 两栋颜色不同且距离最远的房子 反证法贪心 遍历
- 300. 最长递增子序列 dp 贪心+二分
- 169. 多数元素 Boyer-Moore 投票算法
- 91. 解码方法 状态转移
- 260. 只出现一次的数字 III
lsb := xorSum & -xorSum防止溢出 - 547. 省份数量 并查集
static const int n = 205;
int father[n];
void init() {
for (int i = 0; i < n; i++) {
father[i] = i;
}
}
int find(int x) {
if (father[x] == x) {
return x;
}
father[x] = find(father[x]);
return father[x];
}
void join(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
father[x] = y;
}
bool isSame(int x, int y) {
return find(x) == find(y);
}
- 1722. 执行交换操作后的最小汉明距离 维护根索引->(值->数量)的哈希表
- 2615. 等值距离和 哈希表+前缀和 不开longlong见祖宗
- 2833. 距离原点最远的点 贪心 题目有歧义
- 1391. 检查网格中是否存在有效路径 bfs/dfs
- 322. 零钱兑换 多重背包
- 738. 单调递增的数字 技巧
- 2033. 获取单值网格的最小操作数 贪心 中位数
- 233. 数字 1 的个数 数位dp
- 788. 旋转数字 数位dp / 暴力
- 796. 旋转字符串 mod或者substr(kmp)
- 48. 旋转图像 两次swap
- 61. 旋转链表 先成环
- 1861. 旋转盒子 双指针
Page views: Loading... · Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号