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

题目:

题解:

cpp 复制代码
#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] == '+') {
                ops[opsSize++] = ADDITION;
            } 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));
                        if (ops[k] == ADDITION) {
                            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;
}
相关推荐
linhhanpy几秒前
自制操作系统(九、操作系统完整实现)
c语言·开发语言·汇编·c++·操作系统·自制操作系统
矛取矛求1 小时前
string接口的深度理解(内附思维导图)
c语言·开发语言·c++·接口·string
南东山人1 小时前
关于c语言内存越界及防范措施
c语言
一直学习永不止步1 小时前
LeetCode题练习与总结:迷你语法分析器--385
java·数据结构·算法·leetcode·字符串··深度优先搜索
stm 学习ing1 小时前
FPGA 第二讲 初始FPGA
c语言·开发语言·stm32·fpga开发·c#·visual studio·嵌入式实时数据库
Dragonlongbo1 小时前
leetcode01 --- 环形链表判定
算法·leetcode·职场和发展
-cc- Lynn1 小时前
链表类算法【leetcode】
算法·leetcode·链表
学无止境\n2 小时前
[c语言]strcat函数的使用和模拟实现
c语言·开发语言·算法
五味香2 小时前
Linux命令学习,git命令
linux·c语言·开发语言·c++·git·学习·算法
邂逅you2 小时前
深度优先搜索之全排列问题(C语言版)
c语言·数据结构·学习·算法·贪心算法·深度优先