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;
}
相关推荐
蒙奇D索大1 小时前
【数据结构】第六章启航:图论入门——从零掌握有向图、无向图与简单图
c语言·数据结构·考研·改行学it
烂蜻蜓2 小时前
C 语言中的递归:概念、应用与实例解析
c语言·数据结构·算法
javaisC4 小时前
c语言数据结构--------拓扑排序和逆拓扑排序(Kahn算法和DFS算法实现)
c语言·算法·深度优先
爱爬山的老虎4 小时前
【面试经典150题】LeetCode121·买卖股票最佳时机
数据结构·算法·leetcode·面试·职场和发展
雾月554 小时前
LeetCode 914 卡牌分组
java·开发语言·算法·leetcode·职场和发展
想跑步的小弱鸡4 小时前
Leetcode hot 100(day 4)
算法·leetcode·职场和发展
Fantasydg4 小时前
DAY 35 leetcode 202--哈希表.快乐数
算法·leetcode·散列表
jyyyx的算法博客4 小时前
Leetcode 2337 -- 双指针 | 脑筋急转弯
算法·leetcode
ゞ 正在缓冲99%…5 小时前
leetcode76.最小覆盖子串
java·算法·leetcode·字符串·双指针·滑动窗口
小郝 小郝5 小时前
【C语言】strstr查找字符串函数
c语言·开发语言