diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220120\351\241\265\347\254\25412\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220120\351\241\265\347\254\25412\351\242\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..94a30256a104f2f21d1c1372302aa25f70704978 --- /dev/null +++ "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220120\351\241\265\347\254\25412\351\242\230.cpp" @@ -0,0 +1,180 @@ +typedef int QDataType; +typedef struct QListNode +{ + struct QListNode* next; + QDataType data; +}QNode; +// 队列的结构 +typedef struct Queue +{ + QNode* front; + QNode* rear; +}Queue; +// 初始化队列 +void QueueInit(Queue* q) +{ + assert(q); + q->front = NULL; + q->rear = NULL; +} +// 队尾入队列 +void QueuePush(Queue* q, QDataType data) +{ + if (q->front == NULL) + { + QNode* newnode = (QNode*)malloc(sizeof(QNode)); + newnode->next = NULL; + newnode->data = data; + q->front = newnode; + q->rear = newnode; + } + else + { + QNode* newnode = (QNode*)malloc(sizeof(QNode)); + newnode->next = NULL; + newnode->data = data; + q->rear->next = newnode; + q->rear = q->rear->next; + } +} +// 队头出队列 +void QueuePop(Queue* q) +{ + assert(q); + assert(q->front); + QNode* erase = q->front; + q->front = q->front->next; + if (q->rear == erase) + { + q->rear = NULL; + } + free(erase); +} +// 获取队列头部元素 +QDataType QueueFront(Queue* q) +{ + assert(q); + assert(q->front); + return q->front->data; +} +// 获取队列队尾元素 +QDataType QueueBack(Queue* q) +{ + assert(q); + assert(q->front); + return q->rear->data; +} +// 检测队列是否为空,如果为空返回非零结果,如果非空返回0 +int QueueEmpty(Queue* q) +{ + if (q->front == NULL) + { + return 1; + } + else + { + return 0; + } +} +// 销毁队列 +void QueueDestroy(Queue* q) +{ + while (q->front) + { + QNode* erase = q->front; + q->front = q->front->next; + free(erase); + } + q->rear = NULL; +} +//栈的结构定义 +typedef struct { + Queue* queue1; + Queue* queue2; +} MyStack; +//栈的创建 +MyStack* myStackCreate() { + MyStack* tmp=(MyStack*)malloc(sizeof(MyStack)); + tmp->queue1=(Queue*)malloc(sizeof(Queue)); + tmp->queue2=(Queue*)malloc(sizeof(Queue)); + QueueInit(tmp->queue1); + QueueInit(tmp->queue2); + return tmp; +} +//栈的插入 +void myStackPush(MyStack* obj, int x) { + if(QueueEmpty(obj->queue2)) + QueuePush(obj->queue1,x); + else + QueuePush(obj->queue2,x); +} +//栈的删除 +int myStackPop(MyStack* obj) { + assert(!QueueEmpty(obj->queue1)||!QueueEmpty(obj->queue2)); + if(QueueEmpty(obj->queue1)) + { + while(obj->queue2->front!=obj->queue2->rear) + { + QDataType tmp=QueueFront(obj->queue2); + QueuePop(obj->queue2); + QueuePush(obj->queue1,tmp); + } + QDataType ret=QueueFront(obj->queue2); + QueuePop(obj->queue2); + return ret; + } + else + { + while(obj->queue1->front!=obj->queue1->rear) + { + QDataType tmp=QueueFront(obj->queue1); + QueuePop(obj->queue1); + QueuePush(obj->queue2,tmp); + } + QDataType ret=QueueFront(obj->queue1); + QueuePop(obj->queue1); + return ret; + } +} +//获取栈顶元素 +int myStackTop(MyStack* obj) { + assert(!QueueEmpty(obj->queue1)||!QueueEmpty(obj->queue2)); + if(QueueEmpty(obj->queue1)) + { + while(obj->queue2->front!=obj->queue2->rear) + { + QDataType tmp=QueueFront(obj->queue2); + QueuePop(obj->queue2); + QueuePush(obj->queue1,tmp); + } + QDataType ret=QueueFront(obj->queue2); + QueuePop(obj->queue2); + QueuePush(obj->queue1,ret); + return ret; + } + else + { + while(obj->queue1->front!=obj->queue1->rear) + { + QDataType tmp=QueueFront(obj->queue1); + QueuePop(obj->queue1); + QueuePush(obj->queue2,tmp); + } + QDataType ret=QueueFront(obj->queue1); + QueuePop(obj->queue1); + QueuePush(obj->queue2,ret); + return ret; + } +} +//栈的判空 +bool myStackEmpty(MyStack* obj) { + if(QueueEmpty(obj->queue1)&&QueueEmpty(obj->queue2)) + return true; + return false; +} +//栈的销毁 +void myStackFree(MyStack* obj) { + QueueDestroy(obj->queue1); + QueueDestroy(obj->queue2); + free(obj); +} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220120\351\241\265\347\254\25413\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220120\351\241\265\347\254\25413\351\242\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d5930d11936be97693e0ba0ffdd2b964b5c43db1 --- /dev/null +++ "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220120\351\241\265\347\254\25413\351\242\230.cpp" @@ -0,0 +1,132 @@ +typedef int STDataType; +typedef struct Stack +{ + STDataType* a; + int top; // 栈顶 + int capacity; // 容量 +}Stack; +// 初始化栈 +void StackInit(Stack* ps) +{ + STDataType* tmp = (STDataType*)malloc(sizeof(STDataType) * 4); + ps->a = tmp; + ps->top = 0; + ps->capacity = 4; +} +// 入栈 +void StackPush(Stack* ps, STDataType data) +{ + if (ps->top == ps->capacity) + { + STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2); + ps->a = tmp; + ps->capacity *= 2; + } + (ps->a)[ps->top] = data; + ps->top++; +} +// 出栈 +void StackPop(Stack* ps) +{ + assert(ps->top); + ps->top--; +} +// 获取栈顶元素 +STDataType StackTop(Stack* ps) +{ + assert(ps->top); + return (ps->a)[ps->top - 1]; +} +// 获取栈中有效元素个数 +int StackSize(Stack* ps) +{ + return ps->top; +} +// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 +int StackEmpty(Stack* ps) +{ + if (ps->top == 0) + { + return 1; + } + else + { + return 0; + } +} +// 销毁栈 +void StackDestroy(Stack* ps) +{ + free(ps->a); + ps->top = 0; + ps->capacity = 0; +} +//队列的定义 +typedef struct { + Stack* Stack1;//入队列栈 + Stack* Stack2;//出队列栈 +} MyQueue; +//队列的创建 +MyQueue* myQueueCreate() +{ + Stack* pstack1=(Stack*)malloc(sizeof(Stack)); + Stack* pstack2=(Stack*)malloc(sizeof(Stack)); + StackInit(pstack1); + StackInit(pstack2); + MyQueue* queue=(MyQueue*)malloc(sizeof(MyQueue)); + queue->Stack1=pstack1; + queue->Stack2=pstack2; + return queue; +} +//队列的插入 +void myQueuePush(MyQueue* obj, int x) { + if(StackEmpty(obj->Stack1)) + { + while(!StackEmpty(obj->Stack2)) + { + int top=StackTop(obj->Stack2); + StackPush(obj->Stack1,top); + StackPop(obj->Stack2); + } + } + StackPush(obj->Stack1,x); +} +//队列的删除 +int myQueuePop(MyQueue* obj) { + if(StackEmpty(obj->Stack2)) + { + while(!StackEmpty(obj->Stack1)) + { + int top=StackTop(obj->Stack1); + StackPush(obj->Stack2,top); + StackPop(obj->Stack1); + } + } + int top=StackTop(obj->Stack2); + StackPop(obj->Stack2); + return top; +} +//返回队列头元素 +int myQueuePeek(MyQueue* obj) { + if(StackEmpty(obj->Stack2)) + { + while(!StackEmpty(obj->Stack1)) + { + int top=StackTop(obj->Stack1); + StackPush(obj->Stack2,top); + StackPop(obj->Stack1); + } + } + int top=StackTop(obj->Stack2); + return top; +} +//队列的判空 +bool myQueueEmpty(MyQueue* obj) { + return StackEmpty(obj->Stack1)&&StackEmpty(obj->Stack2); +} +//队列的销毁 +void myQueueFree(MyQueue* obj) { + StackDestroy(obj->Stack1); + StackDestroy(obj->Stack2); + free(obj); +} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2541\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2541\351\242\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7613c71793423bae9bdb66cf2bf0d7a69a259908 --- /dev/null +++ "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2541\351\242\230.cpp" @@ -0,0 +1,31 @@ +#include +#include +#include +#include +bool isPalindrome(char *s) { + int len = strlen(s); + int left = 0; + int right = len - 1; + while (left < right) { + // 跳过非字母数字字符 + while (left < right && !isalnum(s[left])) { + left++; + } + while (left < right && !isalnum(s[right])) { + right--; + } + // 将字符转换为小写进行比较 + if (tolower(s[left]) != tolower(s[right])) { + return false; + } + left++; + right--; + } + return true; +} +int main() { + char s[] = "A man, a plan, a canal: Panama"; + bool result = isPalindrome(s); + printf("%s\n", result ? "true" : "false"); + return 0; +} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2542\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2542\351\242\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bb6c04baafa2ab0363ce69639eb4ac3a5c8481e5 --- /dev/null +++ "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2542\351\242\230.cpp" @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +using namespace std; +string longestCommonPrefix(vector& strs) { + if (strs.empty()) return ""; + string res = strs[0];//取向量容器中的第一个字符串 + for (const string& s : strs) + { + for (int i = 0; i < res.size(); i++) + { + if (s[i] != res[i]) + { + //substr用法: s.substr(i,j)表示从下标为i的位置开始截取j位。 + res = res.substr(0, i); + break; + } + } + } + printf("公共前缀为:%s\n", res.c_str()); + return res; +} +string longestCommonPrefix2(vector& strs) { + if (strs.size() == 0) + { + printf("不存在公共前缀"); + return ""; + } + char c; + string res = strs[0]; + for (int i = 0; i < strs[0].size(); i++) { + c = strs[0][i]; + for (int j = 1; j < strs.size(); j++) { + if (i == strs[j].size() || c != strs[j][i]) + { + res = strs[0].substr(0, i); + printf("公共前缀为:%s\n", res.c_str()); + return strs[0].substr(0, i); + } + + } + } + return strs[0]; +} +int main() +{ + vector vec;//定义个一个字符串容器 + vec.push_back("abcdzhangsan");//把字符串"..."压进容器 + vec.push_back("abcdlisi"); + vec.push_back("abcde"); + printf("输入为:\n"); + for (int i = 0; i < vec.size(); i++) { + cout << vec[i] << endl;//打印容器的内容 + } + longestCommonPrefix(vec); + vector vec2; + vec2.push_back("flzhangsan"); + vec2.push_back("fllisi"); + vec2.push_back("flwangwu"); + printf("输入为:\n"); + for (int i = 0; i < vec2.size(); i++) { + cout << vec2[i] << endl; + } + longestCommonPrefix2(vec2); + return 0; +} \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2545\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2545\351\242\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..6b0823283c6e9b60648b9b55f24b1c772fa1dbb5 --- /dev/null +++ "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220138\351\241\265\347\254\2545\351\242\230.cpp" @@ -0,0 +1,21 @@ +class Solution { +public: + bool repeatedSubstringPattern(string s) { + int n = s.size(); + for (int i = 1; i * 2 <= n; ++i) { + if (n % i == 0) { + bool match = true; + for (int j = i; j < n; ++j) { + if (s[j] != s[j - i]) { + match = false; + break; + } + } + if (match) { + return true; + } + } + } + return false; + } +}; \ No newline at end of file diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220159\351\241\265\347\254\2541\351\242\230.cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220159\351\241\265\347\254\2541\351\242\230.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d354d3e9df4294b2b7c0ae1d8a65503401074b92 --- /dev/null +++ "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220159\351\241\265\347\254\2541\351\242\230.cpp" @@ -0,0 +1,17 @@ +#include +int fib(int n) +{ + if (n <= 2) + { + return 1; + } + return fib(n - 1) + fib(n - 2); +} +int main() +{ + int n; + scanf("%d", &n); + int ret = fib(n); + printf("%d\n", ret); + return 0; +} diff --git "a/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220160\351\241\265\347\254\2544\351\242\230,cpp" "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220160\351\241\265\347\254\2544\351\242\230,cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0234ce9d551f9ed1250a589a1e15bf7db7af3abd --- /dev/null +++ "b/2209040021/\346\225\260\346\215\256\347\273\223\346\236\204\346\225\231\347\250\213\344\275\234\344\270\232/\346\225\231\346\235\220160\351\241\265\347\254\2544\351\242\230,cpp" @@ -0,0 +1,42 @@ +bool isPalindrome(struct ListNode* head) { + struct ListNode* cur = head; + + // 先算出链表的长度 + int len = 0; + while (cur) { + len++; + cur = cur->next; + } + + // 创建数组 + int* temp = (int*)malloc(len * sizeof(int)); + if (NULL == temp) { + perror("malloc fail"); + exit(-1); + } + + // 将链表的值复制到数组 + cur = head; + int i = 0; + while (cur) { + temp[i] = cur->val; + cur = cur->next; + i++; + } + + // 使用双指针判断 + int left = 0; + int right = len - 1; + while (left < right) { + if (temp[left] != temp[right]) { + free(temp); + temp = NULL; + return false; + } + left++; + right--; + } + free(temp); + temp = NULL; + return true; +} diff --git a/compile_flags.txt b/compile_flags.txt deleted file mode 100644 index a3ab6cfb07df481b0e9dab4c5e9f6a6b476a2b11..0000000000000000000000000000000000000000 --- a/compile_flags.txt +++ /dev/null @@ -1,2 +0,0 @@ ---language=c++ --std=c++20 \ No newline at end of file