Skip to content

Commit c4ce33d

Browse files
author
kwu130
committed
feat(src): add 71-simplifyPath, 72-minDistance
1 parent c9e5ef5 commit c4ce33d

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎src/71-simplifyPath.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Created by 1412 on 2025/11/10.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
string simplifyPath(string path) {
12+
string res;
13+
istringstream iss(path);
14+
vector<string> st;
15+
string str;
16+
while (getline(iss, str, '/')) {
17+
if (str == "" || str == ".") continue;
18+
if (str == "..") {
19+
if (!st.empty()) {
20+
st.pop_back();
21+
}
22+
} else {
23+
st.push_back(str);
24+
}
25+
}
26+
for (const auto& s : st) {
27+
res += "/" + s;
28+
}
29+
return res.empty() ? "/" : res;
30+
}
31+
};
32+
33+
void test() {
34+
Solution s;
35+
assert(s.simplifyPath("/home/") == "/home");
36+
assert(s.simplifyPath("/home//foo/") == "/home/foo");
37+
assert(s.simplifyPath("/home/user/Documents/../Pictures") == "/home/user/Pictures");
38+
assert(s.simplifyPath("/../") == "/");
39+
assert(s.simplifyPath("/.../a/../b/c/../d/./") == "/.../b/d");
40+
}
41+
42+
int main() {
43+
test();
44+
return 0;
45+
}

‎src/72-minDistance.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Created by 1412 on 2025/11/10.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
int minDistance(string word1, string word2) {
12+
const int m = word1.size(), n = word2.size();
13+
vector<vector<int>> dp(m + 1, vector<int>(n + 1));
14+
for (int i = 0; i <= m; i++) {
15+
dp[i][0] = i;
16+
}
17+
for (int j = 0; j <= n; j++) {
18+
dp[0][j] = j;
19+
}
20+
for (int i = 1; i <= m; i++) {
21+
for (int j = 1; j <= n; j++) {
22+
if (word1[i - 1] == word2[j - 1]) {
23+
dp[i][j] = dp[i - 1][j - 1];
24+
} else {
25+
// 分别对应 替换 插入 删除
26+
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
27+
}
28+
}
29+
}
30+
return dp[m][n];
31+
}
32+
};
33+
34+
void test() {
35+
Solution s;
36+
assert(s.minDistance("horse", "ros") == 3);
37+
assert(s.minDistance("intention", "execution") == 5);
38+
}
39+
40+
int main() {
41+
test();
42+
return 0;
43+
}

0 commit comments

Comments
 (0)