@@ -2423,6 +2423,12 @@ public int numberOfArithmeticSlices(int[] A) {
24232423
24242424[ Leetcode : 583. Delete Operation for Two Strings (Medium)] ( https://leetcode.com/problems/delete-operation-for-two-strings/description/ )
24252425
2426+ ``` html
2427+ Input: "sea", "eat"
2428+ Output: 2
2429+ Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
2430+ ```
2431+
24262432可以转换为求两个字符串的最长公共子序列问题。
24272433
24282434``` java
@@ -2432,8 +2438,8 @@ public int minDistance(String word1, String word2) {
24322438 for (int i = 0 ; i <= m; i++ ) {
24332439 for (int j = 0 ; j <= n; j++ ) {
24342440 if (i == 0 || j == 0 ) continue ;
2435- dp[i][j] = word1. charAt(i - 1 ) == word2. charAt(j - 1 ) ? dp[i - 1 ][j - 1 ] + 1
2436- : Math . max(dp[i][j - 1 ], dp[i - 1 ][j]);
2441+ dp[i][j] = word1. charAt(i - 1 ) == word2. charAt(j - 1 ) ?
2442+ dp[i - 1 ][j - 1 ] + 1 : Math . max(dp[i][j - 1 ], dp[i - 1 ][j]);
24372443 }
24382444 }
24392445 return m + n - 2 * dp[m][n];
@@ -2610,26 +2616,16 @@ public int maxProfit(int[] prices) {
26102616}
26112617```
26122618
2613- ** 统计从 0 \~ n 每个数的二进制表示中 1 的个数**
2614-
2615- [ Leetcode : 338. Counting Bits (Medium)] ( https://leetcode.com/problems/counting-bits/description/ )
2616-
2617- 对于数字 6(110),它可以看成是数字 2(10) 前面加上一个 1 ,因此 dp[ i] = dp[ i&(i-1)] + 1;
2618-
2619- ``` java
2620- public int [] countBits(int num) {
2621- int [] ret = new int [num + 1 ];
2622- for (int i = 1 ; i <= num; i++ ){
2623- ret[i] = ret[i& (i- 1 )] + 1 ;
2624- }
2625- return ret;
2626- }
2627- ```
2628-
26292619** 一组整数对能够构成的最长链**
26302620
26312621[ Leetcode : 646. Maximum Length of Pair Chain (Medium)] ( https://leetcode.com/problems/maximum-length-of-pair-chain/description/ )
26322622
2623+ ``` html
2624+ Input: [[1,2], [2,3], [3,4]]
2625+ Output: 2
2626+ Explanation: The longest chain is [1,2] -> [3,4]
2627+ ```
2628+
26332629对于 (a, b) 和 (c, d) ,如果 b < c,则它们可以构成一条链。
26342630
26352631``` java
@@ -3092,6 +3088,22 @@ public int[] productExceptSelf(int[] nums) {
30923088}
30933089```
30943090
3091+ ** 统计从 0 \~ n 每个数的二进制表示中 1 的个数**
3092+
3093+ [ Leetcode : 338. Counting Bits (Medium)] ( https://leetcode.com/problems/counting-bits/description/ )
3094+
3095+ 对于数字 6(110),它可以看成是数字 (10) 前面加上一个 1 ,因此 dp[ i] = dp[ i&(i-1)] + 1;
3096+
3097+ ``` java
3098+ public int [] countBits(int num) {
3099+ int [] ret = new int [num + 1 ];
3100+ for (int i = 1 ; i <= num; i++ ){
3101+ ret[i] = ret[i& (i- 1 )] + 1 ;
3102+ }
3103+ return ret;
3104+ }
3105+ ```
3106+
30953107# 数据结构相关
30963108
30973109## 栈和队列
0 commit comments