Skip to content

Commit 769ed70

Browse files
committed
auto commit
1 parent 7591a39 commit 769ed70

File tree

1 file changed

+88
-54
lines changed

1 file changed

+88
-54
lines changed

notes/Leetcode 题解.md

Lines changed: 88 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,46 +2327,31 @@ Note that different sequences are counted as different combinations.
23272327
Therefore the output is 7.
23282328
```
23292329

2330+
完全背包。
2331+
23302332
```java
23312333
public int combinationSum4(int[] nums, int target) {
23322334
if (nums == null || nums.length == 0) return 0;
23332335
int[] dp = new int[target + 1];
23342336
dp[0] = 1;
23352337
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];
23392341
}
23402342
}
23412343
}
23422344
return dp[target];
23432345
}
23442346
```
23452347

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-
23642348
**只能进行 k 次的股票交易**
23652349

23662350
[Leetcode : 188. Best Time to Buy and Sell Stock IV (Hard)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
23672351

23682352
```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]))
23702355
```
23712356

23722357
```java
@@ -2392,6 +2377,24 @@ public int maxProfit(int k, int[] prices) {
23922377
}
23932378
```
23942379

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+
23952398
### 数组区间
23962399

23972400
**数组区间和**
@@ -2824,8 +2827,7 @@ public int countPrimes(int n) {
28242827

28252828
```java
28262829
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);
28292831
}
28302832
```
28312833

@@ -2841,47 +2843,80 @@ int lcm(int a, int b){
28412843

28422844
对于 a 和 b 的最大公约数 f(a, b),有:
28432845

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);
28482850

28492851
乘 2 和除 2 都可以转换为移位操作。
28502852

28512853
### 进制转换
28522854

2853-
Java 中 static String toString(int num, int radix) 可以将一个整数装换为 redix 进制表示的字符串。
2854-
28552855
**7 进制**
28562856

28572857
[Leetcode : 504. Base 7 (Easy)](https://leetcode.com/problems/base-7/description/)
28582858

28592859
```java
28602860
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 + "";
28672863
return convertToBase7(num / 7) + num % 7;
28682864
}
28692865
```
28702866

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+
28712890
**16 进制**
28722891

28732892
[Leetcode : 405. Convert a Number to Hexadecimal (Easy)](https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/)
28742893

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+
28752910
```java
28762911
public String toHex(int num) {
28772912
char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
28782913
if(num == 0) return "0";
2879-
String ret = "";
2914+
StringBuilder sb = new StringBuilder();
28802915
while(num != 0){
2881-
ret = map[(num & 0b1111)] + ret;
2882-
num >>>= 4;
2916+
sb.append(map[num & 0b1111]);
2917+
num >>>= 4; // 无符号右移,左边填 0
28832918
}
2884-
return ret;
2919+
return sb.reverse().toString();
28852920
}
28862921
```
28872922

@@ -2918,15 +2953,14 @@ Return "100".
29182953
```java
29192954
public String addBinary(String a, String b) {
29202955
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);
29262961
carry /= 2;
29272962
}
2928-
if(carry == 1) str = "1" + str;
2929-
return str;
2963+
return str.reverse().toString();
29302964
}
29312965
```
29322966

@@ -2938,15 +2972,15 @@ public String addBinary(String a, String b) {
29382972

29392973
```java
29402974
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);
29472981
carry = (x + y + carry) / 10;
29482982
}
2949-
return sb.reverse().toString();
2983+
return str.reverse().toString();
29502984
}
29512985
```
29522986

0 commit comments

Comments
 (0)