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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
添加 0150.逆波兰表达式求值.md C 版本
  • Loading branch information
c-qwer committed Dec 12, 2024
commit d66e733d6c66b37baec8acd0d42f8161a07bf8fc
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 @@ -502,6 +502,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

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