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

Commit 0d48886

Browse files
Merge pull request youngyangyang04#2855 from c-qwer/master
添加0459重复的子字符串 C 版本
2 parents 15c34a2 + 739e3f8 commit 0d48886

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed

‎problems/0150.逆波兰表达式求值.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,67 @@ impl Solution {
489489
}
490490
```
491491

492+
### C:
493+
494+
```c
495+
int str_to_int(char *str) {
496+
// string转integer
497+
int num = 0, tens = 1;
498+
for (int i = strlen(str) - 1; i >= 0; i--) {
499+
if (str[i] == '-') {
500+
num *= -1;
501+
break;
502+
}
503+
num += (str[i] - '0') * tens;
504+
tens *= 10;
505+
}
506+
return num;
507+
}
508+
509+
int evalRPN(char** tokens, int tokensSize) {
510+
511+
int *stack = (int *)malloc(tokensSize * sizeof(int));
512+
assert(stack);
513+
int stackTop = 0;
514+
515+
for (int i = 0; i < tokensSize; i++) {
516+
char symbol = (tokens[i])[0];
517+
if (symbol < '0' && (tokens[i])[1] == '0円') {
518+
519+
// pop两个数字
520+
int num1 = stack[--stackTop];
521+
int num2 = stack[--stackTop];
522+
523+
// 计算结果
524+
int result;
525+
if (symbol == '+') {
526+
result = num1 + num2;
527+
} else if (symbol == '-') {
528+
result = num2 - num1;
529+
} else if (symbol == '/') {
530+
result = num2 / num1;
531+
} else {
532+
result = num1 * num2;
533+
}
534+
535+
// push回stack
536+
stack[stackTop++] = result;
537+
538+
} else {
539+
540+
// push数字进stack
541+
int num = str_to_int(tokens[i]);
542+
stack[stackTop++] = num;
543+
544+
}
545+
}
546+
547+
int result = stack[0];
548+
free(stack);
549+
return result;
550+
}
551+
```
552+
492553
<p align="center">
493554
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
494555
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

‎problems/0225.用队列实现栈.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,95 @@ impl MyStack {
12771277
}
12781278
```
12791279

1280+
### C:
1281+
1282+
> C:单队列
1283+
1284+
```c
1285+
typedef struct Node {
1286+
int val;
1287+
struct Node *next;
1288+
} Node_t;
1289+
1290+
// 用单向链表实现queue
1291+
typedef struct {
1292+
Node_t *head;
1293+
Node_t *foot;
1294+
int size;
1295+
} MyStack;
1296+
1297+
MyStack* myStackCreate() {
1298+
MyStack *obj = (MyStack *)malloc(sizeof(MyStack));
1299+
assert(obj);
1300+
obj->head = NULL;
1301+
obj->foot = NULL;
1302+
obj->size = 0;
1303+
return obj;
1304+
}
1305+
1306+
void myStackPush(MyStack* obj, int x) {
1307+
1308+
Node_t *temp = (Node_t *)malloc(sizeof(Node_t));
1309+
assert(temp);
1310+
temp->val = x;
1311+
temp->next = NULL;
1312+
1313+
// 添加至queue末尾
1314+
if (obj->foot) {
1315+
obj->foot->next = temp;
1316+
} else {
1317+
obj->head = temp;
1318+
}
1319+
obj->foot = temp;
1320+
obj->size++;
1321+
}
1322+
1323+
int myStackPop(MyStack* obj) {
1324+
1325+
// 获取末尾元素
1326+
int target = obj->foot->val;
1327+
1328+
if (obj->head == obj->foot) {
1329+
free(obj->foot);
1330+
obj->head = NULL;
1331+
obj->foot = NULL;
1332+
} else {
1333+
1334+
Node_t *prev = obj->head;
1335+
// 移动至queue尾部节点前一个节点
1336+
while (prev->next != obj->foot) {
1337+
prev = prev->next;
1338+
}
1339+
1340+
free(obj->foot);
1341+
obj->foot = prev;
1342+
obj->foot->next = NULL;
1343+
}
1344+
1345+
obj->size--;
1346+
return target;
1347+
}
1348+
1349+
int myStackTop(MyStack* obj) {
1350+
return obj->foot->val;
1351+
}
1352+
1353+
bool myStackEmpty(MyStack* obj) {
1354+
return obj->size == 0;
1355+
}
1356+
1357+
void myStackFree(MyStack* obj) {
1358+
Node_t *curr = obj->head;
1359+
while (curr != NULL) {
1360+
Node_t *temp = curr->next;
1361+
free(curr);
1362+
curr = temp;
1363+
}
1364+
free(obj);
1365+
}
1366+
1367+
```
1368+
12801369
12811370
<p align="center">
12821371
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/0239.滑动窗口最大值.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,38 @@ public:
890890
};
891891
```
892892
893+
### C
894+
895+
```c
896+
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
897+
*returnSize = numsSize - k + 1;
898+
int *res = (int*)malloc((*returnSize) * sizeof(int));
899+
assert(res);
900+
int *deque = (int*)malloc(numsSize * sizeof(int));
901+
assert(deque);
902+
int front = 0, rear = 0, idx = 0;
903+
904+
for (int i = 0 ; i < numsSize ; i++) {
905+
while (front < rear && deque[front] <= i - k) {
906+
front++;
907+
}
908+
909+
while (front < rear && nums[deque[rear - 1]] <= nums[i]) {
910+
rear--;
911+
}
912+
913+
deque[rear++] = i;
914+
915+
if (i >= k - 1) {
916+
res[idx++] = nums[deque[front]];
917+
}
918+
}
919+
920+
return res;
921+
}
922+
923+
```
924+
893925
<p align="center">
894926
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
895927
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

‎problems/0459.重复的子字符串.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,52 @@ public int[] GetNext(string s)
879879
}
880880
```
881881

882+
### C
883+
884+
```c
885+
// 前缀表不减一
886+
int *build_next(char* s, int len) {
887+
888+
int *next = (int *)malloc(len * sizeof(int));
889+
assert(next);
890+
891+
// 初始化前缀表
892+
next[0] = 0;
893+
894+
// 构建前缀表表
895+
int i = 1, j = 0;
896+
while (i < len) {
897+
if (s[i] == s[j]) {
898+
j++;
899+
next[i] = j;
900+
i++;
901+
} else if (j > 0) {
902+
j = next[j - 1];
903+
} else {
904+
next[i] = 0;
905+
i++;
906+
}
907+
}
908+
return next;
909+
}
910+
911+
bool repeatedSubstringPattern(char* s) {
912+
913+
int len = strlen(s);
914+
int *next = build_next(s, len);
915+
bool result = false;
916+
917+
// 检查最小重复片段能否被长度整除
918+
if (next[len - 1]) {
919+
result = len % (len - next[len - 1]) == 0;
920+
}
921+
922+
free(next);
923+
return result;
924+
}
925+
926+
```
927+
882928
883929
<p align="center">
884930
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

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