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;
}
相关推荐
XH华4 小时前
初识C语言之二维数组(下)
c语言·算法
南宫生5 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
Lenyiin5 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin
Uu_05kkq8 小时前
【C语言1】C语言常见概念(总结复习篇)——库函数、ASCII码、转义字符
c语言·数据结构·算法
嵌入式科普10 小时前
十一、从0开始卷出一个新项目之瑞萨RA6M5串口DTC接收不定长
c语言·stm32·cubeide·e2studio·ra6m5·dma接收不定长
A懿轩A10 小时前
C/C++ 数据结构与算法【栈和队列】 栈+队列详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·栈和队列
1 9 J11 小时前
数据结构 C/C++(实验五:图)
c语言·数据结构·c++·学习·算法
仍然探索未知中12 小时前
C语言经典100例
c语言
涵涵子RUSH12 小时前
合并K个升序链表(最优解)
算法·leetcode
爱吃西瓜的小菜鸡13 小时前
【C语言】矩阵乘法
c语言·学习·算法