diff --git a/prisma/seed.ts b/prisma/seed.ts index 337f886..87a72d8 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -297,11 +297,17 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize) { create: [ { language: "c", - template: ` -#include + template: `#include #include #include +/** + * 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 -#include -#include + template: `#include #include -#include -#include +#include +#include using namespace std; -// 解析输入字符串为整数数组 -vector parseIntArray(string line) { - vector 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& 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 { public: - vector twoSum(vector& nums, int target); + vector twoSum(vector &nums, int target) { + + } }; +vector parseIntArray(const string &input) { + vector 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 &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() { - string line; - while (getline(cin, line)) { - vector nums = parseIntArray(line); + string line; - if (!getline(cin, line)) break; - int target = stoi(line); + while (getline(cin, line)) { + vector nums = parseIntArray(line); - Solution sol; - vector res = sol.twoSum(nums, target); + getline(cin, line); + int target = stoi(line); - cout << formatOutput(res) << endl; - } + Solution sol; + vector result = sol.twoSum(nums, target); - return 0; -} + cout << formatOutput(result) << endl; + } -vector Solution::twoSum(vector& nums, int target) { - - return {}; -} -`, + return 0; +}`, }, ], }, @@ -625,182 +616,162 @@ $(3 → 4 → 2) + (4 → 6 → 5) = 8 → 0 → 7$`, create: [ { language: "c", - template: ` -#include + template: `#include #include #include -// Definition for singly-linked list. struct ListNode { - int val; - struct ListNode *next; + int val; + struct ListNode *next; }; -// 创建链表 -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 *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) { + } -// 打印链表 -void printList(struct ListNode* head) { - printf("["); - while (head) { - printf("%d", head->val); - if (head->next) printf(","); - head = head->next; - } - printf("]\\n"); +struct ListNode *parseList(char *line) { + line[strcspn(line, "\\n")] = 0; + char *p = line; + 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; + cur->next = node; + cur = node; + token = strtok(NULL, ","); + } + + return dummy.next; } -// 释放链表内存 -void freeList(struct ListNode* head) { - while (head) { - struct ListNode* temp = head; - head = head->next; - free(temp); - } +void printList(struct ListNode *head) { + printf("["); + while (head) { + printf("%d", head->val); + head = head->next; + if (head) + printf(","); + } + printf("]\\n"); +} + +void freeList(struct ListNode *head) { + while (head) { + struct ListNode *tmp = head; + head = head->next; + free(tmp); + } } -// 主函数 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* l1 = createList(line); + struct ListNode *result = addTwoNumbers(l1, l2); + printList(result); - if (!fgets(line, sizeof(line), stdin)) break; - struct ListNode* l2 = createList(line); - - struct ListNode* result = addTwoNumbers(l1, l2); - printList(result); - - freeList(l1); - freeList(l2); - freeList(result); - } - - return 0; -} - - -struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { - - return NULL; // 在这里填充你的算法逻辑 -} - -`, + freeList(l1); + freeList(l2); + freeList(result); + } + return 0; +}`, }, { language: "cpp", - template: ` + template: `#include #include -#include #include -#include -#include +#include + using namespace std; -// Definition for singly-linked list. struct ListNode { - int val; - ListNode *next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode *next) : val(x), next(next) {} + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + 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; - dummy.next = nullptr; +ListNode *createList(const string &line) { + ListNode dummy; + ListNode *tail = &dummy; + dummy.next = nullptr; - string nums = line; - nums.erase(remove(nums.begin(), nums.end(), '['), nums.end()); - nums.erase(remove(nums.begin(), nums.end(), ']'), nums.end()); + string nums = line; + nums.erase(remove(nums.begin(), nums.end(), '['), nums.end()); + nums.erase(remove(nums.begin(), nums.end(), ']'), nums.end()); - stringstream ss(nums); - string token; - while (getline(ss, token, ',')) { - if (!token.empty()) { - int val = stoi(token); - tail->next = new ListNode(val); - tail = tail->next; - } + stringstream ss(nums); + string token; + while (getline(ss, token, ',')) { + if (!token.empty()) { + int val = stoi(token); + tail->next = new ListNode(val); + tail = tail->next; } + } - return dummy.next; + return dummy.next; } -// 打印链表 -void printList(ListNode* head) { - cout << "["; - while (head) { - cout << head->val; - if (head->next) cout << ","; - head = head->next; - } - cout << "]" << endl; +void printList(ListNode *head) { + cout << "["; + while (head) { + cout << head->val; + if (head->next) + cout << ","; + head = head->next; + } + cout << "]" << endl; } -// 释放内存 -void freeList(ListNode* head) { - while (head) { - ListNode* tmp = head; - head = head->next; - delete tmp; - } +void freeList(ListNode *head) { + while (head) { + ListNode *tmp = head; + head = head->next; + delete tmp; + } } -// 主函数 int main() { - string line; - while (getline(cin, line)) { - ListNode* l1 = createList(line); - if (!getline(cin, line)) break; - ListNode* l2 = createList(line); + string line; + while (getline(cin, line)) { + ListNode *l1 = createList(line); + if (!getline(cin, line)) + break; + ListNode *l2 = createList(line); - Solution sol; - ListNode* res = sol.addTwoNumbers(l1, l2); - printList(res); + Solution sol; + ListNode *res = sol.addTwoNumbers(l1, l2); + printList(res); - freeList(l1); - freeList(l2); - freeList(res); - } - return 0; -} - - -ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) { - - return nullptr; // 在这里填充你的算法逻辑 -} -`, + freeList(l1); + freeList(l2); + freeList(res); + } + return 0; +}`, }, ], }, @@ -992,117 +963,104 @@ Let $m$ be the size of array \`nums1\` and $n$ be the size of array \`nums2\`. create: [ { language: "c", - template: ` -#include + template: `#include #include #include -// 解析输入数组 -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) { + } -double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size); +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]; + 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; // 在这里填充你的算法逻辑 -} -`, + return 0; +}`, }, { language: "cpp", - template: ` -#include -#include -#include + template: `#include #include -#include +#include +#include + using namespace std; class Solution { public: - double findMedianSortedArrays(vector& nums1, vector& nums2); + double findMedianSortedArrays(vector &nums1, vector &nums2) { + + } }; -// 解析输入为整数数组 -vector 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 result; - stringstream ss(trimmed); - string token; - while (getline(ss, token, ',')) { - if (!token.empty()) { - result.push_back(stoi(token)); - } +vector parseVector(const string &line) { + vector result; + 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; + } + return result; } int main() { - string line; - while (getline(cin, line)) { - vector nums1 = parseIntArray(line); - if (!getline(cin, line)) break; - vector nums2 = parseIntArray(line); + string line; + while (getline(cin, line)) { + vector nums1 = parseVector(line); - Solution sol; - double result = sol.findMedianSortedArrays(nums1, nums2); - printf("%.5f\\n", result); - } - return 0; -} + if (!getline(cin, line)) + break; + vector nums2 = parseVector(line); - - -double Solution::findMedianSortedArrays(vector& nums1, vector& nums2) { - - return 0.0; // 临时返回值,待填充 -} -`, + Solution sol; + double result = sol.findMedianSortedArrays(nums1, nums2); + printf("%.5f\\n", result); + } + return 0; +}`, }, ], },