Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 8 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Oct 2, 2022
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
27 changes: 27 additions & 0 deletions problems/0072.编辑距离.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,33 @@ function minDistance(word1: string, word2: string): number {
};
```

C:

```c
int min(int num1, int num2, int num3) {
return num1 > num2 ? (num2 > num3 ? num3 : num2) : (num1 > num3 ? num3 : num1);
}

int minDistance(char * word1, char * word2){
int dp[strlen(word1)+1][strlen(word2)+1];
dp[0][0] = 0;
for (int i = 1; i <= strlen(word1); i++) dp[i][0] = i;
for (int i = 1; i <= strlen(word2); i++) dp[0][i] = i;

for (int i = 1; i <= strlen(word1); i++) {
for (int j = 1; j <=strlen(word2); j++) {
if (word1[i-1] == word2[j-1]) {
dp[i][j] = dp[i-1][j-1];
}
else {
dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1;
}
}
}
return dp[strlen(word1)][strlen(word2)];
}
```



-----------------------
Expand Down
11 changes: 7 additions & 4 deletions problems/0150.逆波兰表达式求值.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,29 @@ C++代码如下:
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
// 力扣修改了后台测试数据,需要用longlong
stack<long long> st;
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int num1 = st.top();
long long num1 = st.top();
st.pop();
int num2 = st.top();
long long num2 = st.top();
st.pop();
if (tokens[i] == "+") st.push(num2 + num1);
if (tokens[i] == "-") st.push(num2 - num1);
if (tokens[i] == "*") st.push((long)num2 * (long)num1); //力扣改了后台测试数据
if (tokens[i] == "/") st.push(num2 / num1);
} else {
st.push(stoi(tokens[i]));
st.push(stoll(tokens[i]));
}
}

int result = st.top();
st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
return result;
}
};

```

## 题外话
Expand Down
4 changes: 3 additions & 1 deletion problems/0583.两个字符串的删除操作.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ dp[i][j]:以i-1为结尾的字符串word1,和以j-1位结尾的字符串word

那最后当然是取最小值,所以当word1[i - 1] 与 word2[j - 1]不相同的时候,递推公式:dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1});

因为dp[i - 1][j - 1] + 1等于 dp[i - 1][j] 或 dp[i][j - 1],所以递推公式可简化为:dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);


3. dp数组如何初始化

Expand Down Expand Up @@ -90,7 +92,7 @@ public:
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min({dp[i - 1][j - 1] + 2, dp[i - 1][j] + 1, dp[i][j - 1] + 1});
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
}
}
}
Expand Down

AltStyle によって変換されたページ (->オリジナル) /