auto commit
This commit is contained in:
parent
756e99ee4e
commit
1dd4d45e0a
|
@ -1,45 +1,42 @@
|
|||
<!-- GFM-TOC -->
|
||||
* [斐波那契数列](#斐波那契数列)
|
||||
* [爬楼梯](#爬楼梯)
|
||||
* [强盗抢劫](#强盗抢劫)
|
||||
* [强盗在环形街区抢劫](#强盗在环形街区抢劫)
|
||||
* [信件错排](#信件错排)
|
||||
* [母牛生产](#母牛生产)
|
||||
* [1. 爬楼梯](#1-爬楼梯)
|
||||
* [2. 强盗抢劫](#2-强盗抢劫)
|
||||
* [3. 强盗在环形街区抢劫](#3-强盗在环形街区抢劫)
|
||||
* [4. 信件错排](#4-信件错排)
|
||||
* [5. 母牛生产](#5-母牛生产)
|
||||
* [矩阵路径](#矩阵路径)
|
||||
* [矩阵的最小路径和](#矩阵的最小路径和)
|
||||
* [矩阵的总路径数](#矩阵的总路径数)
|
||||
* [1. 矩阵的最小路径和](#1-矩阵的最小路径和)
|
||||
* [2. 矩阵的总路径数](#2-矩阵的总路径数)
|
||||
* [数组区间](#数组区间)
|
||||
* [数组区间和](#数组区间和)
|
||||
* [数组中等差递增子区间的个数](#数组中等差递增子区间的个数)
|
||||
* [1. 数组区间和](#1-数组区间和)
|
||||
* [2. 数组中等差递增子区间的个数](#2-数组中等差递增子区间的个数)
|
||||
* [分割整数](#分割整数)
|
||||
* [分割整数的最大乘积](#分割整数的最大乘积)
|
||||
* [按平方数来分割整数](#按平方数来分割整数)
|
||||
* [分割整数构成字母字符串](#分割整数构成字母字符串)
|
||||
* [1. 分割整数的最大乘积](#1-分割整数的最大乘积)
|
||||
* [2. 按平方数来分割整数](#2-按平方数来分割整数)
|
||||
* [3. 分割整数构成字母字符串](#3-分割整数构成字母字符串)
|
||||
* [最长递增子序列](#最长递增子序列)
|
||||
* [最长递增子序列](#最长递增子序列)
|
||||
* [一组整数对能够构成的最长链](#一组整数对能够构成的最长链)
|
||||
* [最长摆动子序列](#最长摆动子序列)
|
||||
* [1. 最长递增子序列](#1-最长递增子序列)
|
||||
* [2. 一组整数对能够构成的最长链](#2-一组整数对能够构成的最长链)
|
||||
* [3. 最长摆动子序列](#3-最长摆动子序列)
|
||||
* [最长公共子序列](#最长公共子序列)
|
||||
* [0-1 背包](#0-1-背包)
|
||||
* [空间优化](#空间优化)
|
||||
* [无法使用贪心算法的解释](#无法使用贪心算法的解释)
|
||||
* [变种](#变种)
|
||||
* [划分数组为和相等的两部分](#划分数组为和相等的两部分)
|
||||
* [改变一组数的正负号使得它们的和为一给定数](#改变一组数的正负号使得它们的和为一给定数)
|
||||
* [01 字符构成最多的字符串](#01-字符构成最多的字符串)
|
||||
* [找零钱的最少硬币数](#找零钱的最少硬币数)
|
||||
* [找零钱的硬币数组合](#找零钱的硬币数组合)
|
||||
* [字符串按单词列表分割](#字符串按单词列表分割)
|
||||
* [组合总和](#组合总和)
|
||||
* [1. 划分数组为和相等的两部分](#1-划分数组为和相等的两部分)
|
||||
* [2. 改变一组数的正负号使得它们的和为一给定数](#2-改变一组数的正负号使得它们的和为一给定数)
|
||||
* [3. 01 字符构成最多的字符串](#3-01-字符构成最多的字符串)
|
||||
* [4. 找零钱的最少硬币数](#4-找零钱的最少硬币数)
|
||||
* [5. 找零钱的硬币数组合](#5-找零钱的硬币数组合)
|
||||
* [6. 字符串按单词列表分割](#6-字符串按单词列表分割)
|
||||
* [7. 组合总和](#7-组合总和)
|
||||
* [股票交易](#股票交易)
|
||||
* [需要冷却期的股票交易](#需要冷却期的股票交易)
|
||||
* [需要交易费用的股票交易](#需要交易费用的股票交易)
|
||||
* [只能进行两次的股票交易](#只能进行两次的股票交易)
|
||||
* [只能进行 k 次的股票交易](#只能进行-k-次的股票交易)
|
||||
* [1. 需要冷却期的股票交易](#1-需要冷却期的股票交易)
|
||||
* [2. 需要交易费用的股票交易](#2-需要交易费用的股票交易)
|
||||
* [3. 只能进行两次的股票交易](#3-只能进行两次的股票交易)
|
||||
* [4. 只能进行 k 次的股票交易](#4-只能进行-k-次的股票交易)
|
||||
* [字符串编辑](#字符串编辑)
|
||||
* [删除两个字符串的字符使它们相等](#删除两个字符串的字符使它们相等)
|
||||
* [编辑距离](#编辑距离)
|
||||
* [复制粘贴字符](#复制粘贴字符)
|
||||
* [1. 删除两个字符串的字符使它们相等](#1-删除两个字符串的字符使它们相等)
|
||||
* [2. 编辑距离](#2-编辑距离)
|
||||
* [3. 复制粘贴字符](#3-复制粘贴字符)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
|
@ -47,7 +44,7 @@
|
|||
|
||||
# 斐波那契数列
|
||||
|
||||
## 爬楼梯
|
||||
## 1. 爬楼梯
|
||||
|
||||
[70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/)
|
||||
|
||||
|
@ -78,7 +75,7 @@ public int climbStairs(int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 强盗抢劫
|
||||
## 2. 强盗抢劫
|
||||
|
||||
[198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/)
|
||||
|
||||
|
@ -104,7 +101,7 @@ public int rob(int[] nums) {
|
|||
}
|
||||
```
|
||||
|
||||
## 强盗在环形街区抢劫
|
||||
## 3. 强盗在环形街区抢劫
|
||||
|
||||
[213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/)
|
||||
|
||||
|
@ -131,7 +128,7 @@ private int rob(int[] nums, int first, int last) {
|
|||
}
|
||||
```
|
||||
|
||||
## 信件错排
|
||||
## 4. 信件错排
|
||||
|
||||
题目描述:有 N 个 信 和 信封,它们被打乱,求错误装信方式的数量。
|
||||
|
||||
|
@ -146,7 +143,7 @@ private int rob(int[] nums, int first, int last) {
|
|||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/da1f96b9-fd4d-44ca-8925-fb14c5733388.png" width="350px"> </div><br>
|
||||
|
||||
## 母牛生产
|
||||
## 5. 母牛生产
|
||||
|
||||
[程序员代码面试指南-P181](#)
|
||||
|
||||
|
@ -160,7 +157,7 @@ private int rob(int[] nums, int first, int last) {
|
|||
|
||||
# 矩阵路径
|
||||
|
||||
## 矩阵的最小路径和
|
||||
## 1. 矩阵的最小路径和
|
||||
|
||||
[64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/)
|
||||
|
||||
|
@ -196,7 +193,7 @@ public int minPathSum(int[][] grid) {
|
|||
}
|
||||
```
|
||||
|
||||
## 矩阵的总路径数
|
||||
## 2. 矩阵的总路径数
|
||||
|
||||
[62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/)
|
||||
|
||||
|
@ -235,7 +232,7 @@ public int uniquePaths(int m, int n) {
|
|||
|
||||
# 数组区间
|
||||
|
||||
## 数组区间和
|
||||
## 1. 数组区间和
|
||||
|
||||
[303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/)
|
||||
|
||||
|
@ -267,7 +264,7 @@ class NumArray {
|
|||
}
|
||||
```
|
||||
|
||||
## 数组中等差递增子区间的个数
|
||||
## 2. 数组中等差递增子区间的个数
|
||||
|
||||
[413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/)
|
||||
|
||||
|
@ -302,7 +299,7 @@ public int numberOfArithmeticSlices(int[] A) {
|
|||
|
||||
# 分割整数
|
||||
|
||||
## 分割整数的最大乘积
|
||||
## 1. 分割整数的最大乘积
|
||||
|
||||
[343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/)
|
||||
|
||||
|
@ -321,7 +318,7 @@ public int integerBreak(int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 按平方数来分割整数
|
||||
## 2. 按平方数来分割整数
|
||||
|
||||
[279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/)
|
||||
|
||||
|
@ -357,7 +354,7 @@ private List<Integer> generateSquareList(int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 分割整数构成字母字符串
|
||||
## 3. 分割整数构成字母字符串
|
||||
|
||||
[91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/)
|
||||
|
||||
|
@ -405,7 +402,7 @@ public int numDecodings(String s) {
|
|||
|
||||
对于一个长度为 N 的序列,最长递增子序列并不一定会以 S<sub>N</sub> 为结尾,因此 dp[N] 不是序列的最长递增子序列的长度,需要遍历 dp 数组找出最大值才是所要的结果,max{ dp[i] | 1 <= i <= N} 即为所求。
|
||||
|
||||
## 最长递增子序列
|
||||
## 1. 最长递增子序列
|
||||
|
||||
[300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/)
|
||||
|
||||
|
@ -487,7 +484,7 @@ private int binarySearch(int[] tails, int len, int key) {
|
|||
}
|
||||
```
|
||||
|
||||
## 一组整数对能够构成的最长链
|
||||
## 2. 一组整数对能够构成的最长链
|
||||
|
||||
[646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/)
|
||||
|
||||
|
@ -519,7 +516,7 @@ public int findLongestChain(int[][] pairs) {
|
|||
}
|
||||
```
|
||||
|
||||
## 最长摆动子序列
|
||||
## 3. 最长摆动子序列
|
||||
|
||||
[376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/)
|
||||
|
||||
|
@ -627,7 +624,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
}
|
||||
```
|
||||
|
||||
## 空间优化
|
||||
**空间优化**
|
||||
|
||||
在程序实现时可以对 0-1 背包做优化。观察状态转移方程可以知道,前 i 件物品的状态仅与前 i-1 件物品的状态有关,因此可以将 dp 定义为一维数组,其中 dp[j] 既可以表示 dp[i-1][j] 也可以表示 dp[i][j]。此时,
|
||||
|
||||
|
@ -652,7 +649,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
}
|
||||
```
|
||||
|
||||
## 无法使用贪心算法的解释
|
||||
**无法使用贪心算法的解释**
|
||||
|
||||
0-1 背包问题无法使用贪心算法来求解,也就是说不能按照先添加性价比最高的物品来达到最优,这是因为这种方式可能造成背包空间的浪费,从而无法达到最优。考虑下面的物品和一个容量为 5 的背包,如果先添加物品 0 再添加物品 1,那么只能存放的价值为 16,浪费了大小为 2 的空间。最优的方式是存放物品 1 和物品 2,价值为 22.
|
||||
|
||||
|
@ -662,7 +659,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
| 1 | 2 | 10 | 5 |
|
||||
| 2 | 3 | 12 | 4 |
|
||||
|
||||
## 变种
|
||||
**变种**
|
||||
|
||||
- 完全背包:物品数量为无限个
|
||||
|
||||
|
@ -672,7 +669,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
|
||||
- 其它:物品之间相互约束或者依赖
|
||||
|
||||
## 划分数组为和相等的两部分
|
||||
## 1. 划分数组为和相等的两部分
|
||||
|
||||
[416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/)
|
||||
|
||||
|
@ -712,7 +709,7 @@ private int computeArraySum(int[] nums) {
|
|||
}
|
||||
```
|
||||
|
||||
## 改变一组数的正负号使得它们的和为一给定数
|
||||
## 2. 改变一组数的正负号使得它们的和为一给定数
|
||||
|
||||
[494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/)
|
||||
|
||||
|
@ -784,7 +781,7 @@ private int findTargetSumWays(int[] nums, int start, int S) {
|
|||
}
|
||||
```
|
||||
|
||||
## 01 字符构成最多的字符串
|
||||
## 3. 01 字符构成最多的字符串
|
||||
|
||||
[474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/)
|
||||
|
||||
|
@ -822,7 +819,7 @@ public int findMaxForm(String[] strs, int m, int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 找零钱的最少硬币数
|
||||
## 4. 找零钱的最少硬币数
|
||||
|
||||
[322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/)
|
||||
|
||||
|
@ -865,7 +862,7 @@ public int coinChange(int[] coins, int amount) {
|
|||
}
|
||||
```
|
||||
|
||||
## 找零钱的硬币数组合
|
||||
## 5. 找零钱的硬币数组合
|
||||
|
||||
[518\. Coin Change 2 (Medium)](https://leetcode.com/problems/coin-change-2/description/)
|
||||
|
||||
|
@ -897,7 +894,7 @@ public int change(int amount, int[] coins) {
|
|||
}
|
||||
```
|
||||
|
||||
## 字符串按单词列表分割
|
||||
## 6. 字符串按单词列表分割
|
||||
|
||||
[139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/)
|
||||
|
||||
|
@ -928,7 +925,7 @@ public boolean wordBreak(String s, List<String> wordDict) {
|
|||
}
|
||||
```
|
||||
|
||||
## 组合总和
|
||||
## 7. 组合总和
|
||||
|
||||
[377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/)
|
||||
|
||||
|
@ -971,7 +968,7 @@ public int combinationSum4(int[] nums, int target) {
|
|||
|
||||
# 股票交易
|
||||
|
||||
## 需要冷却期的股票交易
|
||||
## 1. 需要冷却期的股票交易
|
||||
|
||||
[309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
|
||||
|
||||
|
@ -1002,7 +999,7 @@ public int maxProfit(int[] prices) {
|
|||
}
|
||||
```
|
||||
|
||||
## 需要交易费用的股票交易
|
||||
## 2. 需要交易费用的股票交易
|
||||
|
||||
[714. Best Time to Buy and Sell Stock with Transaction Fee (Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
|
||||
|
||||
|
@ -1041,7 +1038,7 @@ public int maxProfit(int[] prices, int fee) {
|
|||
```
|
||||
|
||||
|
||||
## 只能进行两次的股票交易
|
||||
## 3. 只能进行两次的股票交易
|
||||
|
||||
[123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
|
||||
|
||||
|
@ -1067,7 +1064,7 @@ public int maxProfit(int[] prices) {
|
|||
}
|
||||
```
|
||||
|
||||
## 只能进行 k 次的股票交易
|
||||
## 4. 只能进行 k 次的股票交易
|
||||
|
||||
[188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
|
||||
|
||||
|
@ -1097,7 +1094,7 @@ public int maxProfit(int k, int[] prices) {
|
|||
|
||||
# 字符串编辑
|
||||
|
||||
## 删除两个字符串的字符使它们相等
|
||||
## 1. 删除两个字符串的字符使它们相等
|
||||
|
||||
[583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/)
|
||||
|
||||
|
@ -1126,7 +1123,7 @@ public int minDistance(String word1, String word2) {
|
|||
}
|
||||
```
|
||||
|
||||
## 编辑距离
|
||||
## 2. 编辑距离
|
||||
|
||||
[72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/)
|
||||
|
||||
|
@ -1179,7 +1176,7 @@ public int minDistance(String word1, String word2) {
|
|||
}
|
||||
```
|
||||
|
||||
## 复制粘贴字符
|
||||
## 3. 复制粘贴字符
|
||||
|
||||
[650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/)
|
||||
|
||||
|
|
|
@ -1,45 +1,42 @@
|
|||
<!-- GFM-TOC -->
|
||||
* [斐波那契数列](#斐波那契数列)
|
||||
* [爬楼梯](#爬楼梯)
|
||||
* [强盗抢劫](#强盗抢劫)
|
||||
* [强盗在环形街区抢劫](#强盗在环形街区抢劫)
|
||||
* [信件错排](#信件错排)
|
||||
* [母牛生产](#母牛生产)
|
||||
* [1. 爬楼梯](#1-爬楼梯)
|
||||
* [2. 强盗抢劫](#2-强盗抢劫)
|
||||
* [3. 强盗在环形街区抢劫](#3-强盗在环形街区抢劫)
|
||||
* [4. 信件错排](#4-信件错排)
|
||||
* [5. 母牛生产](#5-母牛生产)
|
||||
* [矩阵路径](#矩阵路径)
|
||||
* [矩阵的最小路径和](#矩阵的最小路径和)
|
||||
* [矩阵的总路径数](#矩阵的总路径数)
|
||||
* [1. 矩阵的最小路径和](#1-矩阵的最小路径和)
|
||||
* [2. 矩阵的总路径数](#2-矩阵的总路径数)
|
||||
* [数组区间](#数组区间)
|
||||
* [数组区间和](#数组区间和)
|
||||
* [数组中等差递增子区间的个数](#数组中等差递增子区间的个数)
|
||||
* [1. 数组区间和](#1-数组区间和)
|
||||
* [2. 数组中等差递增子区间的个数](#2-数组中等差递增子区间的个数)
|
||||
* [分割整数](#分割整数)
|
||||
* [分割整数的最大乘积](#分割整数的最大乘积)
|
||||
* [按平方数来分割整数](#按平方数来分割整数)
|
||||
* [分割整数构成字母字符串](#分割整数构成字母字符串)
|
||||
* [1. 分割整数的最大乘积](#1-分割整数的最大乘积)
|
||||
* [2. 按平方数来分割整数](#2-按平方数来分割整数)
|
||||
* [3. 分割整数构成字母字符串](#3-分割整数构成字母字符串)
|
||||
* [最长递增子序列](#最长递增子序列)
|
||||
* [最长递增子序列](#最长递增子序列)
|
||||
* [一组整数对能够构成的最长链](#一组整数对能够构成的最长链)
|
||||
* [最长摆动子序列](#最长摆动子序列)
|
||||
* [1. 最长递增子序列](#1-最长递增子序列)
|
||||
* [2. 一组整数对能够构成的最长链](#2-一组整数对能够构成的最长链)
|
||||
* [3. 最长摆动子序列](#3-最长摆动子序列)
|
||||
* [最长公共子序列](#最长公共子序列)
|
||||
* [0-1 背包](#0-1-背包)
|
||||
* [空间优化](#空间优化)
|
||||
* [无法使用贪心算法的解释](#无法使用贪心算法的解释)
|
||||
* [变种](#变种)
|
||||
* [划分数组为和相等的两部分](#划分数组为和相等的两部分)
|
||||
* [改变一组数的正负号使得它们的和为一给定数](#改变一组数的正负号使得它们的和为一给定数)
|
||||
* [01 字符构成最多的字符串](#01-字符构成最多的字符串)
|
||||
* [找零钱的最少硬币数](#找零钱的最少硬币数)
|
||||
* [找零钱的硬币数组合](#找零钱的硬币数组合)
|
||||
* [字符串按单词列表分割](#字符串按单词列表分割)
|
||||
* [组合总和](#组合总和)
|
||||
* [1. 划分数组为和相等的两部分](#1-划分数组为和相等的两部分)
|
||||
* [2. 改变一组数的正负号使得它们的和为一给定数](#2-改变一组数的正负号使得它们的和为一给定数)
|
||||
* [3. 01 字符构成最多的字符串](#3-01-字符构成最多的字符串)
|
||||
* [4. 找零钱的最少硬币数](#4-找零钱的最少硬币数)
|
||||
* [5. 找零钱的硬币数组合](#5-找零钱的硬币数组合)
|
||||
* [6. 字符串按单词列表分割](#6-字符串按单词列表分割)
|
||||
* [7. 组合总和](#7-组合总和)
|
||||
* [股票交易](#股票交易)
|
||||
* [需要冷却期的股票交易](#需要冷却期的股票交易)
|
||||
* [需要交易费用的股票交易](#需要交易费用的股票交易)
|
||||
* [只能进行两次的股票交易](#只能进行两次的股票交易)
|
||||
* [只能进行 k 次的股票交易](#只能进行-k-次的股票交易)
|
||||
* [1. 需要冷却期的股票交易](#1-需要冷却期的股票交易)
|
||||
* [2. 需要交易费用的股票交易](#2-需要交易费用的股票交易)
|
||||
* [3. 只能进行两次的股票交易](#3-只能进行两次的股票交易)
|
||||
* [4. 只能进行 k 次的股票交易](#4-只能进行-k-次的股票交易)
|
||||
* [字符串编辑](#字符串编辑)
|
||||
* [删除两个字符串的字符使它们相等](#删除两个字符串的字符使它们相等)
|
||||
* [编辑距离](#编辑距离)
|
||||
* [复制粘贴字符](#复制粘贴字符)
|
||||
* [1. 删除两个字符串的字符使它们相等](#1-删除两个字符串的字符使它们相等)
|
||||
* [2. 编辑距离](#2-编辑距离)
|
||||
* [3. 复制粘贴字符](#3-复制粘贴字符)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
|
||||
|
@ -47,7 +44,7 @@
|
|||
|
||||
# 斐波那契数列
|
||||
|
||||
## 爬楼梯
|
||||
## 1. 爬楼梯
|
||||
|
||||
[70. Climbing Stairs (Easy)](https://leetcode.com/problems/climbing-stairs/description/)
|
||||
|
||||
|
@ -78,7 +75,7 @@ public int climbStairs(int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 强盗抢劫
|
||||
## 2. 强盗抢劫
|
||||
|
||||
[198. House Robber (Easy)](https://leetcode.com/problems/house-robber/description/)
|
||||
|
||||
|
@ -104,7 +101,7 @@ public int rob(int[] nums) {
|
|||
}
|
||||
```
|
||||
|
||||
## 强盗在环形街区抢劫
|
||||
## 3. 强盗在环形街区抢劫
|
||||
|
||||
[213. House Robber II (Medium)](https://leetcode.com/problems/house-robber-ii/description/)
|
||||
|
||||
|
@ -131,7 +128,7 @@ private int rob(int[] nums, int first, int last) {
|
|||
}
|
||||
```
|
||||
|
||||
## 信件错排
|
||||
## 4. 信件错排
|
||||
|
||||
题目描述:有 N 个 信 和 信封,它们被打乱,求错误装信方式的数量。
|
||||
|
||||
|
@ -146,7 +143,7 @@ private int rob(int[] nums, int first, int last) {
|
|||
|
||||
<div align="center"> <img src="pics/da1f96b9-fd4d-44ca-8925-fb14c5733388.png" width="350px"> </div><br>
|
||||
|
||||
## 母牛生产
|
||||
## 5. 母牛生产
|
||||
|
||||
[程序员代码面试指南-P181](#)
|
||||
|
||||
|
@ -160,7 +157,7 @@ private int rob(int[] nums, int first, int last) {
|
|||
|
||||
# 矩阵路径
|
||||
|
||||
## 矩阵的最小路径和
|
||||
## 1. 矩阵的最小路径和
|
||||
|
||||
[64. Minimum Path Sum (Medium)](https://leetcode.com/problems/minimum-path-sum/description/)
|
||||
|
||||
|
@ -196,7 +193,7 @@ public int minPathSum(int[][] grid) {
|
|||
}
|
||||
```
|
||||
|
||||
## 矩阵的总路径数
|
||||
## 2. 矩阵的总路径数
|
||||
|
||||
[62. Unique Paths (Medium)](https://leetcode.com/problems/unique-paths/description/)
|
||||
|
||||
|
@ -235,7 +232,7 @@ public int uniquePaths(int m, int n) {
|
|||
|
||||
# 数组区间
|
||||
|
||||
## 数组区间和
|
||||
## 1. 数组区间和
|
||||
|
||||
[303. Range Sum Query - Immutable (Easy)](https://leetcode.com/problems/range-sum-query-immutable/description/)
|
||||
|
||||
|
@ -267,7 +264,7 @@ class NumArray {
|
|||
}
|
||||
```
|
||||
|
||||
## 数组中等差递增子区间的个数
|
||||
## 2. 数组中等差递增子区间的个数
|
||||
|
||||
[413. Arithmetic Slices (Medium)](https://leetcode.com/problems/arithmetic-slices/description/)
|
||||
|
||||
|
@ -302,7 +299,7 @@ public int numberOfArithmeticSlices(int[] A) {
|
|||
|
||||
# 分割整数
|
||||
|
||||
## 分割整数的最大乘积
|
||||
## 1. 分割整数的最大乘积
|
||||
|
||||
[343. Integer Break (Medim)](https://leetcode.com/problems/integer-break/description/)
|
||||
|
||||
|
@ -321,7 +318,7 @@ public int integerBreak(int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 按平方数来分割整数
|
||||
## 2. 按平方数来分割整数
|
||||
|
||||
[279. Perfect Squares(Medium)](https://leetcode.com/problems/perfect-squares/description/)
|
||||
|
||||
|
@ -357,7 +354,7 @@ private List<Integer> generateSquareList(int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 分割整数构成字母字符串
|
||||
## 3. 分割整数构成字母字符串
|
||||
|
||||
[91. Decode Ways (Medium)](https://leetcode.com/problems/decode-ways/description/)
|
||||
|
||||
|
@ -405,7 +402,7 @@ public int numDecodings(String s) {
|
|||
|
||||
对于一个长度为 N 的序列,最长递增子序列并不一定会以 S<sub>N</sub> 为结尾,因此 dp[N] 不是序列的最长递增子序列的长度,需要遍历 dp 数组找出最大值才是所要的结果,max{ dp[i] | 1 <= i <= N} 即为所求。
|
||||
|
||||
## 最长递增子序列
|
||||
## 1. 最长递增子序列
|
||||
|
||||
[300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence/description/)
|
||||
|
||||
|
@ -487,7 +484,7 @@ private int binarySearch(int[] tails, int len, int key) {
|
|||
}
|
||||
```
|
||||
|
||||
## 一组整数对能够构成的最长链
|
||||
## 2. 一组整数对能够构成的最长链
|
||||
|
||||
[646. Maximum Length of Pair Chain (Medium)](https://leetcode.com/problems/maximum-length-of-pair-chain/description/)
|
||||
|
||||
|
@ -519,7 +516,7 @@ public int findLongestChain(int[][] pairs) {
|
|||
}
|
||||
```
|
||||
|
||||
## 最长摆动子序列
|
||||
## 3. 最长摆动子序列
|
||||
|
||||
[376. Wiggle Subsequence (Medium)](https://leetcode.com/problems/wiggle-subsequence/description/)
|
||||
|
||||
|
@ -627,7 +624,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
}
|
||||
```
|
||||
|
||||
## 空间优化
|
||||
**空间优化**
|
||||
|
||||
在程序实现时可以对 0-1 背包做优化。观察状态转移方程可以知道,前 i 件物品的状态仅与前 i-1 件物品的状态有关,因此可以将 dp 定义为一维数组,其中 dp[j] 既可以表示 dp[i-1][j] 也可以表示 dp[i][j]。此时,
|
||||
|
||||
|
@ -652,7 +649,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
}
|
||||
```
|
||||
|
||||
## 无法使用贪心算法的解释
|
||||
**无法使用贪心算法的解释**
|
||||
|
||||
0-1 背包问题无法使用贪心算法来求解,也就是说不能按照先添加性价比最高的物品来达到最优,这是因为这种方式可能造成背包空间的浪费,从而无法达到最优。考虑下面的物品和一个容量为 5 的背包,如果先添加物品 0 再添加物品 1,那么只能存放的价值为 16,浪费了大小为 2 的空间。最优的方式是存放物品 1 和物品 2,价值为 22.
|
||||
|
||||
|
@ -662,7 +659,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
| 1 | 2 | 10 | 5 |
|
||||
| 2 | 3 | 12 | 4 |
|
||||
|
||||
## 变种
|
||||
**变种**
|
||||
|
||||
- 完全背包:物品数量为无限个
|
||||
|
||||
|
@ -672,7 +669,7 @@ public int knapsack(int W, int N, int[] weights, int[] values) {
|
|||
|
||||
- 其它:物品之间相互约束或者依赖
|
||||
|
||||
## 划分数组为和相等的两部分
|
||||
## 1. 划分数组为和相等的两部分
|
||||
|
||||
[416. Partition Equal Subset Sum (Medium)](https://leetcode.com/problems/partition-equal-subset-sum/description/)
|
||||
|
||||
|
@ -712,7 +709,7 @@ private int computeArraySum(int[] nums) {
|
|||
}
|
||||
```
|
||||
|
||||
## 改变一组数的正负号使得它们的和为一给定数
|
||||
## 2. 改变一组数的正负号使得它们的和为一给定数
|
||||
|
||||
[494. Target Sum (Medium)](https://leetcode.com/problems/target-sum/description/)
|
||||
|
||||
|
@ -784,7 +781,7 @@ private int findTargetSumWays(int[] nums, int start, int S) {
|
|||
}
|
||||
```
|
||||
|
||||
## 01 字符构成最多的字符串
|
||||
## 3. 01 字符构成最多的字符串
|
||||
|
||||
[474. Ones and Zeroes (Medium)](https://leetcode.com/problems/ones-and-zeroes/description/)
|
||||
|
||||
|
@ -822,7 +819,7 @@ public int findMaxForm(String[] strs, int m, int n) {
|
|||
}
|
||||
```
|
||||
|
||||
## 找零钱的最少硬币数
|
||||
## 4. 找零钱的最少硬币数
|
||||
|
||||
[322. Coin Change (Medium)](https://leetcode.com/problems/coin-change/description/)
|
||||
|
||||
|
@ -865,7 +862,7 @@ public int coinChange(int[] coins, int amount) {
|
|||
}
|
||||
```
|
||||
|
||||
## 找零钱的硬币数组合
|
||||
## 5. 找零钱的硬币数组合
|
||||
|
||||
[518\. Coin Change 2 (Medium)](https://leetcode.com/problems/coin-change-2/description/)
|
||||
|
||||
|
@ -897,7 +894,7 @@ public int change(int amount, int[] coins) {
|
|||
}
|
||||
```
|
||||
|
||||
## 字符串按单词列表分割
|
||||
## 6. 字符串按单词列表分割
|
||||
|
||||
[139. Word Break (Medium)](https://leetcode.com/problems/word-break/description/)
|
||||
|
||||
|
@ -928,7 +925,7 @@ public boolean wordBreak(String s, List<String> wordDict) {
|
|||
}
|
||||
```
|
||||
|
||||
## 组合总和
|
||||
## 7. 组合总和
|
||||
|
||||
[377. Combination Sum IV (Medium)](https://leetcode.com/problems/combination-sum-iv/description/)
|
||||
|
||||
|
@ -971,7 +968,7 @@ public int combinationSum4(int[] nums, int target) {
|
|||
|
||||
# 股票交易
|
||||
|
||||
## 需要冷却期的股票交易
|
||||
## 1. 需要冷却期的股票交易
|
||||
|
||||
[309. Best Time to Buy and Sell Stock with Cooldown(Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
|
||||
|
||||
|
@ -1002,7 +999,7 @@ public int maxProfit(int[] prices) {
|
|||
}
|
||||
```
|
||||
|
||||
## 需要交易费用的股票交易
|
||||
## 2. 需要交易费用的股票交易
|
||||
|
||||
[714. Best Time to Buy and Sell Stock with Transaction Fee (Medium)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
|
||||
|
||||
|
@ -1041,7 +1038,7 @@ public int maxProfit(int[] prices, int fee) {
|
|||
```
|
||||
|
||||
|
||||
## 只能进行两次的股票交易
|
||||
## 3. 只能进行两次的股票交易
|
||||
|
||||
[123. Best Time to Buy and Sell Stock III (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
|
||||
|
||||
|
@ -1067,7 +1064,7 @@ public int maxProfit(int[] prices) {
|
|||
}
|
||||
```
|
||||
|
||||
## 只能进行 k 次的股票交易
|
||||
## 4. 只能进行 k 次的股票交易
|
||||
|
||||
[188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
|
||||
|
||||
|
@ -1097,7 +1094,7 @@ public int maxProfit(int k, int[] prices) {
|
|||
|
||||
# 字符串编辑
|
||||
|
||||
## 删除两个字符串的字符使它们相等
|
||||
## 1. 删除两个字符串的字符使它们相等
|
||||
|
||||
[583. Delete Operation for Two Strings (Medium)](https://leetcode.com/problems/delete-operation-for-two-strings/description/)
|
||||
|
||||
|
@ -1126,7 +1123,7 @@ public int minDistance(String word1, String word2) {
|
|||
}
|
||||
```
|
||||
|
||||
## 编辑距离
|
||||
## 2. 编辑距离
|
||||
|
||||
[72. Edit Distance (Hard)](https://leetcode.com/problems/edit-distance/description/)
|
||||
|
||||
|
@ -1179,7 +1176,7 @@ public int minDistance(String word1, String word2) {
|
|||
}
|
||||
```
|
||||
|
||||
## 复制粘贴字符
|
||||
## 3. 复制粘贴字符
|
||||
|
||||
[650. 2 Keys Keyboard (Medium)](https://leetcode.com/problems/2-keys-keyboard/description/)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user