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