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;
// 解析输入字符串为整数数组
vector<int> parseIntArray(string line) {
vector<int> result;
line.erase(remove(line.begin(), line.end(), '['), line.end());
line.erase(remove(line.begin(), line.end(), ']'), line.end());
stringstream ss(line);
string token;
while (getline(ss, token, ',')) {
if (!token.empty()) {
result.push_back(stoi(token));
}
}
return result;
}
// 格式化输出结果为字符串
string formatOutput(const vector<int>& res) {
if (res.empty()) return "[]";
stringstream ss;
ss << "[";
for (size_t i = 0; i < res.size(); ++i) {
ss << res[i];
if (i != res.size() - 1)
ss << ",";
}
ss << "]";
return ss.str();
}
// Solution 类声明
class Solution { class Solution {
public: public:
vector<int> twoSum(vector<int>& nums, int target); vector<int> twoSum(vector<int> &nums, int target) {
}
}; };
vector<int> parseIntArray(const string &input) {
vector<int> result;
string trimmed = input.substr(1, input.size() - 2);
stringstream ss(trimmed);
string token;
while (getline(ss, token, ',')) {
result.push_back(stoi(token));
}
return result;
}
string formatOutput(const vector<int> &output) {
if (output.empty())
return "[]";
stringstream ss;
ss << "[";
for (size_t i = 0; i < output.size(); ++i) {
ss << output[i];
if (i != output.size() - 1)
ss << ",";
}
ss << "]";
return ss.str();
}
int main() { int main() {
string line; string line;
while (getline(cin, line)) {
vector<int> nums = parseIntArray(line);
if (!getline(cin, line)) break; while (getline(cin, line)) {
int target = stoi(line); vector<int> nums = parseIntArray(line);
Solution sol; getline(cin, line);
vector<int> res = sol.twoSum(nums, target); int target = stoi(line);
cout << formatOutput(res) << endl; Solution sol;
} vector<int> result = sol.twoSum(nums, target);
return 0; cout << formatOutput(result) << endl;
} }
vector<int> Solution::twoSum(vector<int>& nums, int target) { return 0;
}`,
return {};
}
`,
}, },
], ],
}, },
@ -625,182 +616,162 @@ $(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;
line[strcspn(line, "\\n")] = 0;
char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']')) p++;
char *token = strtok(p, ",");
while (token) {
struct ListNode *node = malloc(sizeof(struct ListNode));
node->val = atoi(token);
node->next = NULL;
tail->next = node;
tail = node;
token = strtok(NULL, ",");
}
return dummy.next;
} }
// 打印链表 struct ListNode *parseList(char *line) {
void printList(struct ListNode* head) { line[strcspn(line, "\\n")] = 0;
printf("["); char *p = line;
while (head) { while (*p && (*p == '[' || *p == ' ' || *p == ']'))
printf("%d", head->val); p++;
if (head->next) printf(",");
head = head->next; struct ListNode dummy;
} struct ListNode *cur = &dummy;
printf("]\\n"); dummy.next = NULL;
char *token = strtok(p, ",");
while (token) {
struct ListNode *node = malloc(sizeof(struct ListNode));
node->val = atoi(token);
node->next = NULL;
cur->next = node;
cur = node;
token = strtok(NULL, ",");
}
return dummy.next;
} }
// 释放链表内存 void printList(struct ListNode *head) {
void freeList(struct ListNode* head) { printf("[");
while (head) { while (head) {
struct ListNode* temp = head; printf("%d", head->val);
head = head->next; head = head->next;
free(temp); if (head)
} printf(",");
}
printf("]\\n");
}
void freeList(struct ListNode *head) {
while (head) {
struct ListNode *tmp = head;
head = head->next;
free(tmp);
}
} }
// 主函数
int main() { int main() {
char line[1024]; char line[1024];
while (fgets(line, sizeof(line), stdin)) {
struct ListNode *l1 = parseList(line);
if (!fgets(line, sizeof(line), stdin))
break;
struct ListNode *l2 = parseList(line);
while (fgets(line, sizeof(line), stdin)) { struct ListNode *result = addTwoNumbers(l1, l2);
struct ListNode* l1 = createList(line); printList(result);
if (!fgets(line, sizeof(line), stdin)) break; freeList(l1);
struct ListNode* l2 = createList(line); freeList(l2);
freeList(result);
struct ListNode* result = addTwoNumbers(l1, l2); }
printList(result); return 0;
}`,
freeList(l1);
freeList(l2);
freeList(result);
}
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;
ListNode() : val(0), next(nullptr) {} ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {}
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; dummy.next = nullptr;
dummy.next = nullptr;
string nums = line; string nums = line;
nums.erase(remove(nums.begin(), nums.end(), '['), nums.end()); nums.erase(remove(nums.begin(), nums.end(), '['), nums.end());
nums.erase(remove(nums.begin(), nums.end(), ']'), nums.end()); nums.erase(remove(nums.begin(), nums.end(), ']'), nums.end());
stringstream ss(nums); stringstream ss(nums);
string token; string token;
while (getline(ss, token, ',')) { while (getline(ss, token, ',')) {
if (!token.empty()) { if (!token.empty()) {
int val = stoi(token); int val = stoi(token);
tail->next = new ListNode(val); tail->next = new ListNode(val);
tail = tail->next; tail = tail->next;
}
} }
}
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)
if (head->next) cout << ","; 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; head = head->next;
head = head->next; delete tmp;
delete tmp; }
}
} }
// 主函数
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))
ListNode* l2 = createList(line); break;
ListNode *l2 = createList(line);
Solution sol; Solution sol;
ListNode* res = sol.addTwoNumbers(l1, l2); ListNode *res = sol.addTwoNumbers(l1, l2);
printList(res); printList(res);
freeList(l1); freeList(l1);
freeList(l2); freeList(l2);
freeList(res); freeList(res);
} }
return 0; return 0;
} }`,
ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
return nullptr; // 在这里填充你的算法逻辑
}
`,
}, },
], ],
}, },
@ -992,117 +963,104 @@ 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>
// 解析输入数组
int *parseIntArray(char *line, int *len) {
line[strcspn(line, "\\n")] = 0; // 移除换行符
char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']')) p++; // 跳过空格和括号
int capacity = 10;
int *arr = malloc(capacity * sizeof(int)); // 初始分配空间
*len = 0;
char *token = strtok(p, ","); // 分割输入为逗号分隔的整数
while (token) {
if (*len >= capacity) { // 扩展数组大小
capacity *= 2;
arr = realloc(arr, capacity * sizeof(int));
}
arr[(*len)++] = atoi(token); // 存储整数
token = strtok(NULL, ",");
}
return arr;
}
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size);
int main() {
char line[1024];
while (fgets(line, sizeof(line), stdin)) { // 读取第一行
int len1;
int *nums1 = parseIntArray(line, &len1); // 解析数组1
if (!fgets(line, sizeof(line), stdin)) break; // 如果第二行不存在,退出
int len2;
int *nums2 = parseIntArray(line, &len2); // 解析数组2
double result = findMedianSortedArrays(nums1, len1, nums2, len2); // 计算中位数
printf("%.5f\\n", result); // 输出中位数保留5位小数
free(nums1); // 释放内存
free(nums2); // 释放内存
}
return 0;
}
// 寻找中位数函数
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
return 0.0; // 在这里填充你的算法逻辑
} }
`,
int *parseIntArray(char *line, int *len) {
line[strcspn(line, "\\n")] = 0;
char *p = line;
while (*p && (*p == '[' || *p == ' ' || *p == ']'))
p++;
int capacity = 10;
int *arr = malloc(capacity * sizeof(int));
*len = 0;
char *token = strtok(p, ",");
while (token) {
if (*len >= capacity) {
capacity *= 2;
arr = realloc(arr, capacity * sizeof(int));
}
arr[(*len)++] = atoi(token);
token = strtok(NULL, ",");
}
return arr;
}
int main() {
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
int nums1Size;
int *nums1 = parseIntArray(line, &nums1Size);
if (!fgets(line, sizeof(line), stdin))
break;
int nums2Size;
int *nums2 = parseIntArray(line, &nums2Size);
double result = findMedianSortedArrays(nums1, nums1Size, nums2, nums2Size);
printf("%.5f\\n", result);
free(nums1);
free(nums2);
}
return 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) { vector<int> result;
string trimmed = line; stringstream ss(line);
trimmed.erase(remove(trimmed.begin(), trimmed.end(), '['), trimmed.end()); char c;
trimmed.erase(remove(trimmed.begin(), trimmed.end(), ']'), trimmed.end()); int num;
while (ss >> c) {
vector<int> result; if (isdigit(c) || c == '-' || c == '+') {
stringstream ss(trimmed); ss.putback(c);
string token; ss >> num;
while (getline(ss, token, ',')) { result.push_back(num);
if (!token.empty()) {
result.push_back(stoi(token));
}
} }
return result; }
return result;
} }
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);
Solution sol; if (!getline(cin, line))
double result = sol.findMedianSortedArrays(nums1, nums2); break;
printf("%.5f\\n", result); vector<int> nums2 = parseVector(line);
}
return 0;
}
Solution sol;
double result = sol.findMedianSortedArrays(nums1, nums2);
double Solution::findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { printf("%.5f\\n", result);
}
return 0.0; // 临时返回值,待填充 return 0;
} }`,
`,
}, },
], ],
}, },