forked from youngyangyang04/leetcode-master
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from youngyangyang04:master #467
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
jenningsloy318
merged 2 commits into
AlgorithmAndLeetCode:master
from
youngyangyang04:master
Jul 26, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
problems/kamacoder/0113.国际象棋.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
# 113.国际象棋 | ||
|
||
广搜,但本题如果广搜枚举马和象的话会超时。 | ||
|
||
广搜要只枚举马的走位,同时判断是否在对角巷直接走象 | ||
|
||
```CPP | ||
#include <iostream> | ||
using namespace std; | ||
const int N = 100005, mod = 1000000007; | ||
using ll = long long; | ||
int n, ans; | ||
int dir[][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, -1}, {-2, 1}}; | ||
int main() { | ||
int x1, y1, x2, y2; | ||
cin >> n; | ||
while (n--) { | ||
scanf("%d%d%d%d", &x1, &y1, &x2, &y2); | ||
if (x1 == x2 && y1 == y2) { | ||
cout << 0 << endl; | ||
continue; | ||
} | ||
// 判断象走一步到达 | ||
int d = abs(x1 - x2) - abs(y1 - y2); | ||
if (!d) {cout << 1 << endl; continue;} | ||
// 判断马走一步到达 | ||
bool one = 0; | ||
for (int i = 0; i < 8; ++i) { | ||
int dx = x1 + dir[i][0], dy = y1 + dir[i][1]; | ||
if (dx == x2 && dy == y2) { | ||
cout << 1 << endl; | ||
one = true; | ||
break; | ||
} | ||
} | ||
if (one) continue; | ||
// 接下来为两步的逻辑, 象走两步或者马走一步,象走一步 | ||
// 象直接两步可以到达,这个计算是不是同颜色的格子,象可以在两步到达所有同颜色的格子 | ||
int d2 = abs(x1 - x2) + abs(y1 - y2); | ||
if (d2 % 2 == 0) { | ||
cout << 2 << endl; | ||
continue; | ||
} | ||
// 接下来判断马 + 象的组合 | ||
bool two = 0; | ||
for (int i = 0; i < 8; ++i) { | ||
int dx = x1 + dir[i][0], dy = y1 + dir[i][1]; | ||
int d = abs(dx - x2) - abs(dy - y2); | ||
if (!d) {cout << 2 << endl; two = true; break;} | ||
} | ||
if (two) continue; | ||
// 剩下的格子全都是三步到达的 | ||
cout << 3 << endl; | ||
} | ||
return 0; | ||
} | ||
|
||
``` |
132 changes: 132 additions & 0 deletions
problems/kamacoder/0121.小红的区间翻转.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
# 121. 小红的区间翻转 | ||
|
||
比较暴力的方式,就是直接模拟, 枚举所有 区间,然后检查其翻转的情况。 | ||
|
||
在检查翻转的时候,需要一些代码优化,否则容易超时。 | ||
|
||
```CPP | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
bool canTransform(const vector<int>& a, const vector<int>& b, int left, int right) { | ||
// 提前检查翻转区间的值是否可以匹配 | ||
for (int i = left, j = right; i <= right; i++, j--) { | ||
if (a[i] != b[j]) { | ||
return false; | ||
} | ||
} | ||
// 检查翻转区间外的值是否匹配 | ||
for (int i = 0; i < left; i++) { | ||
if (a[i] != b[i]) { | ||
return false; | ||
} | ||
} | ||
for (int i = right + 1; i < a.size(); i++) { | ||
if (a[i] != b[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
int main() { | ||
int n; | ||
cin >> n; | ||
|
||
vector<int> a(n); | ||
vector<int> b(n); | ||
|
||
for (int i = 0; i < n; i++) { | ||
cin >> a[i]; | ||
} | ||
|
||
for (int i = 0; i < n; i++) { | ||
cin >> b[i]; | ||
} | ||
|
||
int count = 0; | ||
|
||
// 遍历所有可能的区间 | ||
for (int left = 0; left < n; left++) { | ||
for (int right = left; right < n; right++) { | ||
// 检查翻转区间 [left, right] 后,a 是否可以变成 b | ||
if (canTransform(a, b, left, right)) { | ||
count++; | ||
} | ||
} | ||
} | ||
cout << count << endl; | ||
return 0; | ||
} | ||
``` | ||
|
||
也可以事先计算好,最长公共前缀,和最长公共后缀。 | ||
|
||
在公共前缀和公共后缀之间的部分进行翻转操作,这样我们可以减少很多不必要的翻转尝试。 | ||
|
||
通过在公共前缀和后缀之间的部分,找到可以通过翻转使得 a 和 b 相等的区间。 | ||
|
||
以下 为评论区 卡码网用户:码鬼的C++代码 | ||
|
||
```CPP | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
int n; | ||
cin >> n; | ||
vector<int> a(n), b(n); | ||
for (int i = 0; i < n; i++) { | ||
cin >> a[i]; | ||
} | ||
for (int i = 0; i < n; i++) { | ||
cin >> b[i]; | ||
} | ||
|
||
vector<int> prefix(n, 0), suffix(n, 0); | ||
|
||
// 计算前缀相等的位置 | ||
int p = 0; | ||
while (p < n && a[p] == b[p]) { | ||
prefix[p] = 1; | ||
p++; | ||
} | ||
|
||
// 计算后缀相等的位置 | ||
int s = n - 1; | ||
while (s >= 0 && a[s] == b[s]) { | ||
suffix[s] = 1; | ||
s--; | ||
} | ||
|
||
int count = 0; | ||
|
||
// 遍历所有可能的区间 | ||
for (int i = 0; i < n - 1; i++) { | ||
for (int j = i + 1; j < n; j++) { | ||
// 判断前缀和后缀是否相等 | ||
if ((i == 0 || prefix[i - 1] == 1) && (j == n - 1 || suffix[j + 1] == 1)) { | ||
// 判断翻转后的子数组是否和目标数组相同 | ||
bool is_palindrome = true; | ||
for (int k = 0; k <= (j - i) / 2; k++) { | ||
if (a[i + k] != b[j - k]) { | ||
is_palindrome = false; | ||
break; | ||
} | ||
} | ||
if (is_palindrome) { | ||
count++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
cout << count << endl; | ||
|
||
return 0; | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.