C语言 | Leetcode C语言题解之第241题为运算表达式设计优先级

284 篇文章 1 订阅

#define ADDITION -1
#define SUBTRACTION -2
#define MULTIPLICATION -3

int* diffWaysToCompute(char * expression, int* returnSize) {
int len = strlen(expression);
int *ops = (int *)malloc(sizeof(int) * len);
int opsSize = 0;
for (int i = 0; i < len;) {
if (!isdigit(expression[i])) {
if (expression[i] == '+') {
} else if (expression[i] == '-') {
ops[opsSize++] = SUBTRACTION;
} else {
ops[opsSize++] = MULTIPLICATION;
}
i++;
} else {
int t = 0;
while (i < len && isdigit(expression[i])) {
t = t * 10 + expression[i] - '0';
i++;
}
ops[opsSize++] = t;
}
}
struct ListNode ***dp = NULL;
dp = (struct ListNode ***)malloc(sizeof(struct ListNode **) * opsSize);
for (int i = 0; i < opsSize; i++) {
dp[i] = (struct ListNode **)malloc(sizeof(struct ListNode *) * opsSize);
for (int j = 0; j < opsSize; j++) {
dp[i][j] = NULL;
}
}
for (int i = 0; i < opsSize; i += 2) {
struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = ops[i];
node->next = NULL;
dp[i][i] = node;
}
for (int i = 3; i <= opsSize; i++) {
for (int j = 0; j + i <= opsSize; j += 2) {
int l = j;
int r = j + i - 1;
for (int k = j + 1; k < r; k += 2) {
struct ListNode *left = dp[l][k - 1];
struct ListNode *right = dp[k + 1][r];
for (struct ListNode *left = dp[l][k - 1]; left; left = left->next) {
for (struct ListNode *right = dp[k + 1][r]; right; right = right->next) {
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
node->val = left->val + right->val;
}
else if (ops[k] == SUBTRACTION) {
node->val = left->val - right->val;
}
else if (ops[k] == MULTIPLICATION) {
node->val = left->val * right->val;
}
node->next = dp[l][r];
dp[l][r] = node;
}
}
}
}
}
int *ans = (int *)malloc(sizeof(int) * (1 << opsSize));
int pos = 0;
for (struct ListNode *node = dp[0][opsSize - 1]; node; node = node->next) {
ans[pos++] = node->val;
}
*returnSize = pos;
for (int i = 0; i < opsSize; i++) {
for (int j = 0; j < opsSize; j++) {
struct ListNode *curr, *tmp;
curr = dp[i][j];
while (curr) {
tmp = curr;
curr = curr->next;
free(tmp);
}
}
free(dp[i]);
}
free(dp);
free(ops);
return ans;
}
• 15
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
03-27 6491
11-19 204
11-06 133
12-16 1041
08-03

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。