@@ -2327,46 +2327,31 @@ Note that different sequences are counted as different combinations.
2327
2327
Therefore the output is 7.
2328
2328
```
2329
2329
2330
+ 完全背包。
2331
+
2330
2332
``` java
2331
2333
public int combinationSum4(int [] nums, int target) {
2332
2334
if (nums == null || nums. length == 0 ) return 0 ;
2333
2335
int [] dp = new int [target + 1 ];
2334
2336
dp[0 ] = 1 ;
2335
2337
for (int i = 1 ; i <= target; i++ ) {
2336
- for (int j = 0 ; j < nums. length; j ++ ) {
2337
- if (nums[j] <= i) {
2338
- dp[i] += dp[i - nums[j] ];
2338
+ for (int num : nums) {
2339
+ if (num <= i) {
2340
+ dp[i] += dp[i - num ];
2339
2341
}
2340
2342
}
2341
2343
}
2342
2344
return dp[target];
2343
2345
}
2344
2346
```
2345
2347
2346
- ** 只能进行两次的股票交易**
2347
-
2348
- [ Leetcode : 123. Best Time to Buy and Sell Stock III (Hard)] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ )
2349
-
2350
- ``` java
2351
- public int maxProfit(int [] prices) {
2352
- int firstBuy = Integer . MIN_VALUE , firstSell = 0 ;
2353
- int secondBuy = Integer . MIN_VALUE , secondSell = 0 ;
2354
- for (int curPrice : prices) {
2355
- if (firstBuy < - curPrice) firstBuy = - curPrice;
2356
- if (firstSell < firstBuy + curPrice) firstSell = firstBuy + curPrice;
2357
- if (secondBuy < firstSell - curPrice) secondBuy = firstSell - curPrice;
2358
- if (secondSell < secondBuy + curPrice) secondSell = secondBuy + curPrice;
2359
- }
2360
- return secondSell;
2361
- }
2362
- ```
2363
-
2364
2348
** 只能进行 k 次的股票交易**
2365
2349
2366
2350
[ Leetcode : 188. Best Time to Buy and Sell Stock IV (Hard)] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ )
2367
2351
2368
2352
``` html
2369
- dp[i, j] = max(dp[i, j-1], prices[j] - prices[jj] + dp[i-1, jj]) { jj in range of [0, j-1] } = max(dp[i, j-1], prices[j] + max(dp[i-1, jj] - prices[jj]))
2353
+ dp[i, j] = max(dp[i, j-1], prices[j] - prices[jj] + dp[i-1, jj]) { jj in range of [0, j-1] }
2354
+ = max(dp[i, j-1], prices[j] + max(dp[i-1, jj] - prices[jj]))
2370
2355
```
2371
2356
2372
2357
``` java
@@ -2392,6 +2377,24 @@ public int maxProfit(int k, int[] prices) {
2392
2377
}
2393
2378
```
2394
2379
2380
+ ** 只能进行两次的股票交易**
2381
+
2382
+ [ Leetcode : 123. Best Time to Buy and Sell Stock III (Hard)] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ )
2383
+
2384
+ ``` java
2385
+ public int maxProfit(int [] prices) {
2386
+ int firstBuy = Integer . MIN_VALUE , firstSell = 0 ;
2387
+ int secondBuy = Integer . MIN_VALUE , secondSell = 0 ;
2388
+ for (int curPrice : prices) {
2389
+ if (firstBuy < - curPrice) firstBuy = - curPrice;
2390
+ if (firstSell < firstBuy + curPrice) firstSell = firstBuy + curPrice;
2391
+ if (secondBuy < firstSell - curPrice) secondBuy = firstSell - curPrice;
2392
+ if (secondSell < secondBuy + curPrice) secondSell = secondBuy + curPrice;
2393
+ }
2394
+ return secondSell;
2395
+ }
2396
+ ```
2397
+
2395
2398
### 数组区间
2396
2399
2397
2400
** 数组区间和**
@@ -2824,8 +2827,7 @@ public int countPrimes(int n) {
2824
2827
2825
2828
``` java
2826
2829
int gcd(int a, int b) {
2827
- if (b == 0 ) return a;
2828
- return gcd(b, a % b);
2830
+ return b == 0 ? a : gcd(b, a% b);
2829
2831
}
2830
2832
```
2831
2833
@@ -2841,47 +2843,80 @@ int lcm(int a, int b){
2841
2843
2842
2844
对于 a 和 b 的最大公约数 f(a, b),有:
2843
2845
2844
- 1 \. 如果 a 和 b 均为偶数,f(a, b) = 2\* f(a/2, b/2);
2845
- 2 \. 如果 a 是偶数 b 是奇数,f(a, b) = f(a/2, b);
2846
- 3 \. 如果 b 是偶数 a 是奇数,f(a, b) = f(a, b/2);
2847
- 4 \. 如果 a 和 b 均为奇数,f(a, b) = f(a, a-b);
2846
+ - 如果 a 和 b 均为偶数,f(a, b) = 2\* f(a/2, b/2);
2847
+ - 如果 a 是偶数 b 是奇数,f(a, b) = f(a/2, b);
2848
+ - 如果 b 是偶数 a 是奇数,f(a, b) = f(a, b/2);
2849
+ - 如果 a 和 b 均为奇数,f(a, b) = f(a, a-b);
2848
2850
2849
2851
乘 2 和除 2 都可以转换为移位操作。
2850
2852
2851
2853
### 进制转换
2852
2854
2853
- Java 中 static String toString(int num, int radix) 可以将一个整数装换为 redix 进制表示的字符串。
2854
-
2855
2855
** 7 进制**
2856
2856
2857
2857
[ Leetcode : 504. Base 7 (Easy)] ( https://leetcode.com/problems/base-7/description/ )
2858
2858
2859
2859
``` java
2860
2860
public String convertToBase7(int num) {
2861
- if (num < 0 ) {
2862
- return ' -' + convertToBase7(- num);
2863
- }
2864
- if (num < 7 ) {
2865
- return num + " " ;
2866
- }
2861
+ if (num < 0 ) return ' -' + convertToBase7(- num);
2862
+ if (num < 7 ) return num + " " ;
2867
2863
return convertToBase7(num / 7 ) + num % 7 ;
2868
2864
}
2869
2865
```
2870
2866
2867
+ ``` java
2868
+ public String convertToBase7(int num) {
2869
+ if (num == 0 ) return " 0" ;
2870
+ StringBuilder sb = new StringBuilder ();
2871
+ boolean isNegative = num < 0 ;
2872
+ if (isNegative) num = - num;
2873
+ while (num > 0 ) {
2874
+ sb. append(num % 7 );
2875
+ num /= 7 ;
2876
+ }
2877
+ String ret = sb. reverse(). toString();
2878
+ return isNegative ? " -" + ret : ret;
2879
+ }
2880
+ ```
2881
+
2882
+ Java 中 static String toString(int num, int radix) 可以将一个整数转换为 redix 进制表示的字符串。
2883
+
2884
+ ``` java
2885
+ public String convertToBase7(int num) {
2886
+ return Integer . toString(num, 7 );
2887
+ }
2888
+ ```
2889
+
2871
2890
** 16 进制**
2872
2891
2873
2892
[ Leetcode : 405. Convert a Number to Hexadecimal (Easy)] ( https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/ )
2874
2893
2894
+ 负数要用它的补码形式。
2895
+
2896
+ ``` html
2897
+ Input:
2898
+ 26
2899
+
2900
+ Output:
2901
+ "1a"
2902
+
2903
+ Input:
2904
+ -1
2905
+
2906
+ Output:
2907
+ "ffffffff"
2908
+ ```
2909
+
2875
2910
``` java
2876
2911
public String toHex(int num) {
2877
2912
char [] map = {' 0' ,' 1' ,' 2' ,' 3' ,' 4' ,' 5' ,' 6' ,' 7' ,' 8' ,' 9' ,' a' ,' b' ,' c' ,' d' ,' e' ,' f' };
2878
2913
if (num == 0 ) return " 0" ;
2879
- String ret = " " ;
2914
+ StringBuilder sb = new StringBuilder () ;
2880
2915
while (num != 0 ){
2881
- ret = map[( num & 0b1111 )] + ret ;
2882
- num >>> = 4 ;
2916
+ sb . append( map[num & 0b1111 ]) ;
2917
+ num >>> = 4 ; // 无符号右移,左边填 0
2883
2918
}
2884
- return ret ;
2919
+ return sb . reverse() . toString() ;
2885
2920
}
2886
2921
```
2887
2922
@@ -2918,15 +2953,14 @@ Return "100".
2918
2953
``` java
2919
2954
public String addBinary(String a, String b) {
2920
2955
int i = a. length() - 1 , j = b. length() - 1 , carry = 0 ;
2921
- String str = " " ;
2922
- while ( i >= 0 || j >= 0 ){
2923
- if (i >= 0 && a. charAt(i-- ) == ' 1' ) carry++ ;
2924
- if (j >= 0 && b. charAt(j-- ) == ' 1' ) carry++ ;
2925
- str = (carry % 2 ) + str ;
2956
+ StringBuilder str = new StringBuilder () ;
2957
+ while (carry == 1 || i >= 0 || j >= 0 ) {
2958
+ if (i >= 0 && a. charAt(i-- ) == ' 1' ) carry++ ;
2959
+ if (j >= 0 && b. charAt(j-- ) == ' 1' ) carry++ ;
2960
+ str. append (carry % 2 );
2926
2961
carry /= 2 ;
2927
2962
}
2928
- if (carry == 1 ) str = " 1" + str;
2929
- return str;
2963
+ return str. reverse(). toString();
2930
2964
}
2931
2965
```
2932
2966
@@ -2938,15 +2972,15 @@ public String addBinary(String a, String b) {
2938
2972
2939
2973
``` java
2940
2974
public String addStrings(String num1, String num2) {
2941
- StringBuilder sb = new StringBuilder ();
2942
- int carry = 0 ;
2943
- for ( int i = num1 . length() - 1 , j = num2 . length() - 1 ; i >= 0 || j >= 0 || carry == 1 ; i -- , j -- ) {
2944
- int x = i < 0 ? 0 : num1. charAt(i) - ' 0' ;
2945
- int y = j < 0 ? 0 : num2. charAt(j) - ' 0' ;
2946
- sb . append((x + y + carry) % 10 );
2975
+ StringBuilder str = new StringBuilder ();
2976
+ int carry = 0 , i = num1 . length() - 1 , j = num2 . length() - 1 ;
2977
+ while (carry == 1 || i >= 0 || j >= 0 ) {
2978
+ int x = i < 0 ? 0 : num1. charAt(i-- ) - ' 0' ;
2979
+ int y = j < 0 ? 0 : num2. charAt(j-- ) - ' 0' ;
2980
+ str . append((x + y + carry) % 10 );
2947
2981
carry = (x + y + carry) / 10 ;
2948
2982
}
2949
- return sb . reverse(). toString();
2983
+ return str . reverse(). toString();
2950
2984
}
2951
2985
```
2952
2986
0 commit comments