mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 17:26:32 +08:00
init
This commit is contained in:
32
24/04/1379.cpp
Normal file
32
24/04/1379.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by szh2 on 24-4-3.
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
|
||||
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
};
|
||||
|
||||
|
||||
class Solution{
|
||||
public:
|
||||
TreeNode * getTargetCopy(TreeNode * original, TreeNode * cloned, TreeNode * target) {
|
||||
if (original == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (original == target) {
|
||||
return cloned;
|
||||
}
|
||||
TreeNode *left = getTargetCopy(original->left, cloned->left, target);
|
||||
if (left != nullptr) {
|
||||
return left;
|
||||
}
|
||||
return getTargetCopy(original->right, cloned->right, target);
|
||||
}
|
||||
};
|
||||
34
24/04/2192.cpp
Normal file
34
24/04/2192.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> getAncestors(int n, vector<vector<int>> &edges) {
|
||||
vector<vector<int>> g(n);
|
||||
for (auto &e : edges) {
|
||||
g[e[1]].push_back(e[0]); // 反向建图
|
||||
}
|
||||
|
||||
vector<vector<int>> ans(n);
|
||||
vector<int> vis(n);
|
||||
function<void(int)> dfs = [&](int x) {
|
||||
vis[x] = true; // 避免重复访问
|
||||
for (int y : g[x]) {
|
||||
if (!vis[y]) {
|
||||
dfs(y); // 只递归没有访问过的点
|
||||
}
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < n; i++) {
|
||||
ranges::fill(vis, false);
|
||||
dfs(i); // 从 i 开始 DFS
|
||||
vis[i] = false; // ans[i] 不含 i
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (vis[j]) {
|
||||
ans[i].push_back(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
31
24/04/2810.c
Normal file
31
24/04/2810.c
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/4/3.
|
||||
//
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void reverse(char *s, int end) {
|
||||
int i = 0;
|
||||
while (i < end) {
|
||||
char temp = s[i];
|
||||
s[i] = s[end];
|
||||
s[end] = temp;
|
||||
i++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
|
||||
char *finalString(char *s) {
|
||||
int len = strlen(s);
|
||||
char *r = malloc((len + 1) * sizeof(char));
|
||||
int x = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (i != 0 && s[i] == 'i') {
|
||||
reverse(r, x - 1);
|
||||
} else {
|
||||
r[x++] = s[i];
|
||||
}
|
||||
}
|
||||
r[x] = '\0';
|
||||
return r;
|
||||
}
|
||||
Reference in New Issue
Block a user