Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions problems/0131.分割回文串.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ for (int i = startIndex; i < s.size(); i++) {
continue;
}
backtracking(s, i + 1); // 寻找i+1为起始位置的子串
path.pop_back(); // 回溯过程,弹出本次已经填在的子串
path.pop_back(); // 回溯过程,弹出本次已经添加的子串
}
```

Expand Down Expand Up @@ -189,7 +189,7 @@ private:
continue;
}
backtracking(s, i + 1); // 寻找i+1为起始位置的子串
path.pop_back(); // 回溯过程,弹出本次已经填在的子串
path.pop_back(); // 回溯过程,弹出本次已经添加的子串
}
}
bool isPalindrome(const string& s, int start, int end) {
Expand Down Expand Up @@ -245,7 +245,7 @@ private:
continue;
}
backtracking(s, i + 1); // 寻找i+1为起始位置的子串
path.pop_back(); // 回溯过程,弹出本次已经填在的子串
path.pop_back(); // 回溯过程,弹出本次已经添加的子串
}
}
void computePalindrome(const string& s) {
Expand Down Expand Up @@ -437,7 +437,7 @@ class Solution:
substring = s[startIndex:i + 1]
path.append(substring)
self.backtracking(s, i + 1, path, result, isPalindrome) # 寻找i+1为起始位置的子串
path.pop() # 回溯过程,弹出本次已经填在的子串
path.pop() # 回溯过程,弹出本次已经添加的子串

def computePalindrome(self, s, isPalindrome):
for i in range(len(s) - 1, -1, -1): # 需要倒序计算,保证在i行时,i+1行已经计算好了
Expand Down Expand Up @@ -497,7 +497,7 @@ func dfs(s string, start int) {
if isPalindrome(str) { // 是回文子串
path = append(path, str)
dfs(s, i+1) // 寻找i+1为起始位置的子串
path = path[:len(path)-1] // 回溯过程,弹出本次已经填在的子串
path = path[:len(path)-1] // 回溯过程,弹出本次已经添加的子串
}
}
}
Expand Down
37 changes: 36 additions & 1 deletion problems/0188.买卖股票的最佳时机IV.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Solution {
}
}

//版本三:一维 dp数组
//版本三:一维 dp数组 (下面有和卡哥邏輯一致的一維數組JAVA解法)
class Solution {
public int maxProfit(int k, int[] prices) {
if(prices.length == 0){
Expand Down Expand Up @@ -259,6 +259,41 @@ class Solution {
}
}
```
```JAVA
class Solution {
public int maxProfit(int k, int[] prices) {

//edge cases
if(prices.length == 0 || k == 0)
return 0;


int dp[] = new int [k * 2 + 1];

//和卡哥邏輯一致,奇數天購入股票,故初始化只初始化奇數天。
for(int i = 1; i < 2 * k + 1; i += 2){
dp[i] = -prices[0];
}

for(int i = 1; i < prices.length; i++){ //i 從 1 開始,因爲第 i = 0 天已經透過初始化完成了。
for(int j = 1; j < 2 * k + 1; j++){ //j 從 1 開始,因爲第 j = 0 天已經透過初始化完成了。
//奇數天購買
if(j % 2 == 1)
dp[j] = Math.max(dp[j], dp[j - 1] - prices[i]);
//偶數天賣出
else
dp[j] = Math.max(dp[j], dp[j - 1] + prices[i]);
}
//打印DP數組
//for(int x : dp)
// System.out.print(x +", ");
//System.out.println();
}
//return 第2 * k次賣出的獲利。
return dp[2 * k];
}
}
```

Python:

Expand Down
1 change: 1 addition & 0 deletions problems/0674.最长连续递增序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Java:
dp[i] = 1;
}
int res = 1;
//可以注意到,這邊的 i 是從 0 開始,所以會出現和卡哥的C++ code有差異的地方,在一些地方會看到有 i + 1 的偏移。
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i + 1] > nums[i]) {
dp[i + 1] = dp[i] + 1;
Expand Down