chore(seed): add seed data for problem templates

This commit is contained in:
cfngc4594 2025-04-15 23:07:47 +08:00
parent 59ba12258c
commit 0b3086f333

View File

@ -297,11 +297,17 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
create: [ create: [
{ {
language: "c", language: "c",
template: ` template: `#include <stdio.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int *twoSum(int *nums, int numsSize, int target, int *returnSize) {
}
int *parseIntArray(char *line, int *len) { int *parseIntArray(char *line, int *len) {
line[strcspn(line, "\\n")] = 0; line[strcspn(line, "\\n")] = 0;
char *p = line; char *p = line;
@ -341,8 +347,6 @@ char *formatOutput(int *res, int resLen) {
return buf; return buf;
} }
int *twoSum(int *nums, int numsSize, int target, int *returnSize);
int main() { int main() {
char line[1024]; char line[1024];
while (fgets(line, sizeof(line), stdin)) { while (fgets(line, sizeof(line), stdin)) {
@ -366,84 +370,71 @@ int main() {
free(output); free(output);
} }
return 0; return 0;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
}`, }`,
}, },
{ {
language: "cpp", language: "cpp",
template: ` template: `#include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream> #include <sstream>
#include <algorithm> #include <string>
#include <unordered_map> #include <vector>
using namespace std; using namespace std;
// 解析输入字符串为整数数组 class Solution {
vector<int> parseIntArray(string line) { public:
vector<int> twoSum(vector<int> &nums, int target) {
}
};
vector<int> parseIntArray(const string &input) {
vector<int> result; vector<int> result;
line.erase(remove(line.begin(), line.end(), '['), line.end()); string trimmed = input.substr(1, input.size() - 2);
line.erase(remove(line.begin(), line.end(), ']'), line.end()); stringstream ss(trimmed);
stringstream ss(line);
string token; string token;
while (getline(ss, token, ',')) { while (getline(ss, token, ',')) {
if (!token.empty()) {
result.push_back(stoi(token)); result.push_back(stoi(token));
} }
}
return result; return result;
} }
// 格式化输出结果为字符串 string formatOutput(const vector<int> &output) {
string formatOutput(const vector<int>& res) { if (output.empty())
if (res.empty()) return "[]"; return "[]";
stringstream ss; stringstream ss;
ss << "["; ss << "[";
for (size_t i = 0; i < res.size(); ++i) {
ss << res[i]; for (size_t i = 0; i < output.size(); ++i) {
if (i != res.size() - 1) ss << output[i];
if (i != output.size() - 1)
ss << ","; ss << ",";
} }
ss << "]"; ss << "]";
return ss.str(); return ss.str();
} }
// Solution 类声明
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target);
};
int main() { int main() {
string line; string line;
while (getline(cin, line)) { while (getline(cin, line)) {
vector<int> nums = parseIntArray(line); vector<int> nums = parseIntArray(line);
if (!getline(cin, line)) break; getline(cin, line);
int target = stoi(line); int target = stoi(line);
Solution sol; Solution sol;
vector<int> res = sol.twoSum(nums, target); vector<int> result = sol.twoSum(nums, target);
cout << formatOutput(res) << endl; cout << formatOutput(result) << endl;
} }
return 0; return 0;
} }`,
vector<int> Solution::twoSum(vector<int>& nums, int target) {
return {};
}
`,
}, },
], ],
}, },
@ -625,69 +616,68 @@ $(3 → 4 → 2) + (4 → 6 → 5) = 8 → 0 → 7$`,
create: [ create: [
{ {
language: "c", language: "c",
template: ` template: `#include <stdio.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// Definition for singly-linked list.
struct ListNode { struct ListNode {
int val; int val;
struct ListNode *next; struct ListNode *next;
}; };
// 创建链表 struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) {
struct ListNode* createList(char *line) {
struct ListNode dummy;
struct ListNode *tail = &dummy;
dummy.next = NULL;
}
struct ListNode *parseList(char *line) {
line[strcspn(line, "\\n")] = 0; line[strcspn(line, "\\n")] = 0;
char *p = line; char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']')) p++; while (*p && (*p == '[' || *p == ' ' || *p == ']'))
p++;
struct ListNode dummy;
struct ListNode *cur = &dummy;
dummy.next = NULL;
char *token = strtok(p, ","); char *token = strtok(p, ",");
while (token) { while (token) {
struct ListNode *node = malloc(sizeof(struct ListNode)); struct ListNode *node = malloc(sizeof(struct ListNode));
node->val = atoi(token); node->val = atoi(token);
node->next = NULL; node->next = NULL;
tail->next = node; cur->next = node;
tail = node; cur = node;
token = strtok(NULL, ","); token = strtok(NULL, ",");
} }
return dummy.next; return dummy.next;
} }
// 打印链表
void printList(struct ListNode *head) { void printList(struct ListNode *head) {
printf("["); printf("[");
while (head) { while (head) {
printf("%d", head->val); printf("%d", head->val);
if (head->next) printf(",");
head = head->next; head = head->next;
if (head)
printf(",");
} }
printf("]\\n"); printf("]\\n");
} }
// 释放链表内存
void freeList(struct ListNode *head) { void freeList(struct ListNode *head) {
while (head) { while (head) {
struct ListNode* temp = head; struct ListNode *tmp = head;
head = head->next; head = head->next;
free(temp); free(tmp);
} }
} }
// 主函数
int main() { int main() {
char line[1024]; char line[1024];
while (fgets(line, sizeof(line), stdin)) { while (fgets(line, sizeof(line), stdin)) {
struct ListNode* l1 = createList(line); struct ListNode *l1 = parseList(line);
if (!fgets(line, sizeof(line), stdin))
if (!fgets(line, sizeof(line), stdin)) break; break;
struct ListNode* l2 = createList(line); struct ListNode *l2 = parseList(line);
struct ListNode *result = addTwoNumbers(l1, l2); struct ListNode *result = addTwoNumbers(l1, l2);
printList(result); printList(result);
@ -696,29 +686,18 @@ int main() {
freeList(l2); freeList(l2);
freeList(result); freeList(result);
} }
return 0; return 0;
} }`,
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
return NULL; // 在这里填充你的算法逻辑
}
`,
}, },
{ {
language: "cpp", language: "cpp",
template: ` template: `#include <algorithm>
#include <iostream> #include <iostream>
#include <string>
#include <sstream> #include <sstream>
#include <vector> #include <string>
#include <algorithm>
using namespace std; using namespace std;
// Definition for singly-linked list.
struct ListNode { struct ListNode {
int val; int val;
ListNode *next; ListNode *next;
@ -727,13 +706,13 @@ struct ListNode {
ListNode(int x, ListNode *next) : val(x), next(next) {} ListNode(int x, ListNode *next) : val(x), next(next) {}
}; };
// 声明 Solution 类
class Solution { class Solution {
public: public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2); ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
}
}; };
// 输入字符串 -> 链表
ListNode *createList(const string &line) { ListNode *createList(const string &line) {
ListNode dummy; ListNode dummy;
ListNode *tail = &dummy; ListNode *tail = &dummy;
@ -756,18 +735,17 @@ ListNode* createList(const string& line) {
return dummy.next; return dummy.next;
} }
// 打印链表
void printList(ListNode *head) { void printList(ListNode *head) {
cout << "["; cout << "[";
while (head) { while (head) {
cout << head->val; cout << head->val;
if (head->next) cout << ","; if (head->next)
cout << ",";
head = head->next; head = head->next;
} }
cout << "]" << endl; cout << "]" << endl;
} }
// 释放内存
void freeList(ListNode *head) { void freeList(ListNode *head) {
while (head) { while (head) {
ListNode *tmp = head; ListNode *tmp = head;
@ -776,12 +754,12 @@ void freeList(ListNode* head) {
} }
} }
// 主函数
int main() { int main() {
string line; string line;
while (getline(cin, line)) { while (getline(cin, line)) {
ListNode *l1 = createList(line); ListNode *l1 = createList(line);
if (!getline(cin, line)) break; if (!getline(cin, line))
break;
ListNode *l2 = createList(line); ListNode *l2 = createList(line);
Solution sol; Solution sol;
@ -793,14 +771,7 @@ int main() {
freeList(res); freeList(res);
} }
return 0; return 0;
} }`,
ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
return nullptr; // 在这里填充你的算法逻辑
}
`,
}, },
], ],
}, },
@ -992,91 +963,84 @@ Let $m$ be the size of array \`nums1\` and $n$ be the size of array \`nums2\`.
create: [ create: [
{ {
language: "c", language: "c",
template: ` template: `#include <stdio.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// 解析输入数组 double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
}
int *parseIntArray(char *line, int *len) { int *parseIntArray(char *line, int *len) {
line[strcspn(line, "\\n")] = 0; // 移除换行符 line[strcspn(line, "\\n")] = 0;
char *p = line; char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']')) p++; // 跳过空格和括号 while (*p && (*p == '[' || *p == ' ' || *p == ']'))
p++;
int capacity = 10; int capacity = 10;
int *arr = malloc(capacity * sizeof(int)); // 初始分配空间 int *arr = malloc(capacity * sizeof(int));
*len = 0; *len = 0;
char *token = strtok(p, ","); // 分割输入为逗号分隔的整数 char *token = strtok(p, ",");
while (token) { while (token) {
if (*len >= capacity) { // 扩展数组大小 if (*len >= capacity) {
capacity *= 2; capacity *= 2;
arr = realloc(arr, capacity * sizeof(int)); arr = realloc(arr, capacity * sizeof(int));
} }
arr[(*len)++] = atoi(token); // 存储整数 arr[(*len)++] = atoi(token);
token = strtok(NULL, ","); token = strtok(NULL, ",");
} }
return arr; return arr;
} }
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size);
int main() { int main() {
char line[1024]; char line[1024];
while (fgets(line, sizeof(line), stdin)) { // 读取第一行 while (fgets(line, sizeof(line), stdin)) {
int len1; int nums1Size;
int *nums1 = parseIntArray(line, &len1); // 解析数组1 int *nums1 = parseIntArray(line, &nums1Size);
if (!fgets(line, sizeof(line), stdin)) break; // 如果第二行不存在,退出 if (!fgets(line, sizeof(line), stdin))
int len2; break;
int *nums2 = parseIntArray(line, &len2); // 解析数组2 int nums2Size;
int *nums2 = parseIntArray(line, &nums2Size);
double result = findMedianSortedArrays(nums1, len1, nums2, len2); // 计算中位数 double result = findMedianSortedArrays(nums1, nums1Size, nums2, nums2Size);
printf("%.5f\\n", result); // 输出中位数保留5位小数 printf("%.5f\\n", result);
free(nums1); // 释放内存 free(nums1);
free(nums2); // 释放内存 free(nums2);
} }
return 0; return 0;
} }`,
// 寻找中位数函数
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
return 0.0; // 在这里填充你的算法逻辑
}
`,
}, },
{ {
language: "cpp", language: "cpp",
template: ` template: `#include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream> #include <sstream>
#include <algorithm> #include <string>
#include <vector>
using namespace std; using namespace std;
class Solution { class Solution {
public: public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2); double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) {
}
}; };
// 解析输入为整数数组 vector<int> parseVector(const string &line) {
vector<int> parseIntArray(const string& line) {
string trimmed = line;
trimmed.erase(remove(trimmed.begin(), trimmed.end(), '['), trimmed.end());
trimmed.erase(remove(trimmed.begin(), trimmed.end(), ']'), trimmed.end());
vector<int> result; vector<int> result;
stringstream ss(trimmed); stringstream ss(line);
string token; char c;
while (getline(ss, token, ',')) { int num;
if (!token.empty()) { while (ss >> c) {
result.push_back(stoi(token)); if (isdigit(c) || c == '-' || c == '+') {
ss.putback(c);
ss >> num;
result.push_back(num);
} }
} }
return result; return result;
@ -1085,24 +1049,18 @@ vector<int> parseIntArray(const string& line) {
int main() { int main() {
string line; string line;
while (getline(cin, line)) { while (getline(cin, line)) {
vector<int> nums1 = parseIntArray(line); vector<int> nums1 = parseVector(line);
if (!getline(cin, line)) break;
vector<int> nums2 = parseIntArray(line); if (!getline(cin, line))
break;
vector<int> nums2 = parseVector(line);
Solution sol; Solution sol;
double result = sol.findMedianSortedArrays(nums1, nums2); double result = sol.findMedianSortedArrays(nums1, nums2);
printf("%.5f\\n", result); printf("%.5f\\n", result);
} }
return 0; return 0;
} }`,
double Solution::findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
return 0.0; // 临时返回值,待填充
}
`,
}, },
], ],
}, },