LeetCode-241. Different Ways to Add Parentheses [C++][Java]

LeetCode-241. Different Ways to Add Parentheseshttps://leetcode.com/problems/different-ways-to-add-parentheses/

题目描述

Given a string expression of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. You may return the answer in any order.

Example 1:

Input: expression = "2-1-1"
Output: [0,2]
Explanation:
((2-1)-1) = 0 
(2-(1-1)) = 2

Example 2:

Input: expression = "2*3-4*5"
Output: [-34,-14,-10,-10,10]
Explanation:
(2*(3-(4*5))) = -34 
((2*3)-(4*5)) = -14 
((2*(3-4))*5) = -10 
(2*((3-4)*5)) = -10 
(((2*3)-4)*5) = 10

Constraints:

  • 1 <= expression.length <= 20
  • expression consists of digits and the operator '+''-', and '*'.
  • All the integer values in the input expression are in the range [0, 99].

解题思路

【C++】

1. 分治递归

class Solution {
    unordered_map<string, vector<int>> m;
public:
    vector<int> diffWaysToCompute(string expression) {
        if (m.count(expression)) return m[expression];
        vector<int> ways;
        for (int i = 0; i < expression.length(); ++i) {
            char c = expression[i];
            if (c == '+' || c == '-' || c == '*') {
                vector<int> left = diffWaysToCompute(expression.substr(0, i));
                vector<int> right = diffWaysToCompute(expression.substr(i + 1));
                for (const int & l : left) {
                    for (const int & r : right) {
                        switch (c) {
                            case '+': ways.push_back(l + r); break;
                            case '-': ways.push_back(l - r); break; 
                            case '*': ways.push_back(l * r); break;    
                        }
                    }
                }
            }
        }
        if (ways.empty()) {ways.push_back(stoi(expression));}
        m[expression] = ways;
        return ways;
    }
};

2. 动态规划

class Solution {
public:
    vector<int> diffWaysToCompute(string expression) {
        vector<int> data;
        vector<char> ops;
        int num = 0;
        char op = ' ';
        istringstream ss(expression + "+");
        while (ss >> num && ss >> op) {
            data.push_back(num);
            ops.push_back(op);
        }
        int n = data.size();
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>()));
        for (int i = 0; i < n; ++i) {
            dp[i][i].push_back(data[i]);
            for (int j = i - 1; j >= 0; --j) {
                for (int k = j; k < i; k += 1) {
                    for (auto left : dp[j][k]) {
                        for (auto right : dp[k+1][i]) {
                            int val = 0;
                            switch (ops[k]) {
                                case '+': val = left + right; break;
                                case '-': val = left - right; break;
                                case '*': val = left * right; break;
                            }
                            dp[j][i].push_back(val);
                        }
                    }
                }
            }
        }
        return dp[0][n-1];
    }
};

【Java】

class Solution {
    private Map<String, List<Integer>> m = new ConcurrentHashMap<>();
    public List<Integer> diffWaysToCompute(String expression) {
        if (m.containsKey(expression)) {return m.get(expression);}
        List<Integer> ways = new ArrayList<>();
        for (int i = 0; i < expression.length(); ++i) {
            char c = expression.charAt(i);
            if (c == '+' || c == '-' || c == '*') {
                List<Integer> left = diffWaysToCompute(expression.substring(0, i));
                List<Integer> right = diffWaysToCompute(expression.substring(i + 1));
                for (int l : left) {
                    for (int r : right) {
                        switch (c) {
                            case '+': ways.add(l + r); break;
                            case '-': ways.add(l - r); break; 
                            case '*': ways.add(l * r); break;    
                        }
                    }
                }
            }
        }
        if (ways.isEmpty()) {ways.add(Integer.parseInt(expression));}
        m.put(expression, ways);
        return ways;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值