Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions problems/0150.逆波兰表达式求值.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,67 @@ impl Solution {
}
```

### C:

```c
int str_to_int(char *str) {
// string转integer
int num = 0, tens = 1;
for (int i = strlen(str) - 1; i >= 0; i--) {
if (str[i] == '-') {
num *= -1;
break;
}
num += (str[i] - '0') * tens;
tens *= 10;
}
return num;
}

int evalRPN(char** tokens, int tokensSize) {

int *stack = (int *)malloc(tokensSize * sizeof(int));
assert(stack);
int stackTop = 0;

for (int i = 0; i < tokensSize; i++) {
char symbol = (tokens[i])[0];
if (symbol < '0' && (tokens[i])[1] == '0円') {

// pop两个数字
int num1 = stack[--stackTop];
int num2 = stack[--stackTop];

// 计算结果
int result;
if (symbol == '+') {
result = num1 + num2;
} else if (symbol == '-') {
result = num2 - num1;
} else if (symbol == '/') {
result = num2 / num1;
} else {
result = num1 * num2;
}

// push回stack
stack[stackTop++] = result;

} else {

// push数字进stack
int num = str_to_int(tokens[i]);
stack[stackTop++] = num;
}
}

int result = stack[0];
free(stack);
return result;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
89 changes: 89 additions & 0 deletions problems/0225.用队列实现栈.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,95 @@ impl MyStack {
}
```

### C:

> C:单队列
```c
typedef struct Node {
int val;
struct Node *next;
} Node_t;

// 用单向链表实现queue
typedef struct {
Node_t *head;
Node_t *foot;
int size;
} MyStack;

MyStack* myStackCreate() {
MyStack *obj = (MyStack *)malloc(sizeof(MyStack));
assert(obj);
obj->head = NULL;
obj->foot = NULL;
obj->size = 0;
return obj;
}

void myStackPush(MyStack* obj, int x) {

Node_t *temp = (Node_t *)malloc(sizeof(Node_t));
assert(temp);
temp->val = x;
temp->next = NULL;

// 添加至queue末尾
if (obj->foot) {
obj->foot->next = temp;
} else {
obj->head = temp;
}
obj->foot = temp;
obj->size++;
}

int myStackPop(MyStack* obj) {

// 获取末尾元素
int target = obj->foot->val;

if (obj->head == obj->foot) {
free(obj->foot);
obj->head = NULL;
obj->foot = NULL;
} else {
Node_t *prev = obj->head;
// 移动至queue尾部节点前一个节点
while (prev->next != obj->foot) {
prev = prev->next;
}

free(obj->foot);
obj->foot = prev;
obj->foot->next = NULL;
}

obj->size--;
return target;
}

int myStackTop(MyStack* obj) {
return obj->foot->val;
}

bool myStackEmpty(MyStack* obj) {
return obj->size == 0;
}

void myStackFree(MyStack* obj) {
Node_t *curr = obj->head;
while (curr != NULL) {
Node_t *temp = curr->next;
free(curr);
curr = temp;
}
free(obj);
}

```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
32 changes: 32 additions & 0 deletions problems/0239.滑动窗口最大值.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,38 @@ public:
};
```

### C

```c
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
*returnSize = numsSize - k + 1;
int *res = (int*)malloc((*returnSize) * sizeof(int));
assert(res);
int *deque = (int*)malloc(numsSize * sizeof(int));
assert(deque);
int front = 0, rear = 0, idx = 0;

for (int i = 0 ; i < numsSize ; i++) {
while (front < rear && deque[front] <= i - k) {
front++;
}

while (front < rear && nums[deque[rear - 1]] <= nums[i]) {
rear--;
}

deque[rear++] = i;

if (i >= k - 1) {
res[idx++] = nums[deque[front]];
}
}

return res;
}

```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down
46 changes: 46 additions & 0 deletions problems/0459.重复的子字符串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,52 @@ public int[] GetNext(string s)
}
```

### C

```c
// 前缀表不减一
int *build_next(char* s, int len) {

int *next = (int *)malloc(len * sizeof(int));
assert(next);

// 初始化前缀表
next[0] = 0;

// 构建前缀表表
int i = 1, j = 0;
while (i < len) {
if (s[i] == s[j]) {
j++;
next[i] = j;
i++;
} else if (j > 0) {
j = next[j - 1];
} else {
next[i] = 0;
i++;
}
}
return next;
}

bool repeatedSubstringPattern(char* s) {

int len = strlen(s);
int *next = build_next(s, len);
bool result = false;

// 检查最小重复片段能否被长度整除
if (next[len - 1]) {
result = len % (len - next[len - 1]) == 0;
}

free(next);
return result;
}

```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down

AltStyle によって変換されたページ (->オリジナル) /