LeetCode-241 Different Ways to Add Parentheses

题目描述

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +- and *.

题目大意

给定一个表示算术式的字符串,要求计算该算术式加上括号后可能得到的计算结果。

(算术式只包含 ‘+’ ,‘-’,‘*’ 运算符)

示例

E1

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

E2

Input: "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

解题思路

利用递归的思想,当访问到一个算术符时,将该字符串以该算术符进行分割,分别计算两个子字符串的计算结果,最后计算当前的运算符的计算结果。为了能够减少计算量,可以使用map<string, vector<int> >保存不同子字符串计算得到的可能的计算结果,以免之后重复计算。

复杂度分析

时间复杂度:O(N^2)

空间复杂度:O(N)

代码

class Solution {
public:
    vector<int> diffWaysToCompute(string input) {
        // 用来保存子字符串计算式可得到的所有的计算结果
        map<string, vector<int> > dp;
        return solve(input, dp);
    }
    
    vector<int> solve(string input, map<string, vector<int> >& dp) {
        vector<int> res;
        // 遍历当前字符串,每当遇到一个计算符,进行字符串分割递归调用
        for(int i = 0; i < input.length(); ++i) {
            if(input[i] == '+' || input[i] == '-' || input[i] == '*') {
                string s1, s2;
                vector<int> res1, res2;
                // 将字符串以当前计算符分割为两个子字符串
                s1 = input.substr(0, i);
                s2 = input.substr(i + 1);
                // 若该子字符串以被计算过,则无需计算直接调用,否则需要递归计算
                if(dp.find(s1) != dp.end())
                    res1 = dp[s1];
                else 
                    res1 = solve(s1, dp);
                if(dp.find(s2) != dp.end())
                    res2 = dp[s2];
                else
                    res2 = solve(s2, dp);
                // 对分割的两个子字符串的所有计算结果根据当前计算符进行计算
                for(auto n1 : res1) {
                    for(auto n2 : res2) {
                        switch(input[i]) {
                            case '+' : res.push_back(n1 + n2); break;
                            case '-' : res.push_back(n1 - n2); break;
                            case '*' : res.push_back(n1 * n2); break;
                        }
                    }
                }
            }
        }
        // 若字符串中只包含了数字,则将该数字放入数组返回
        if(res.empty())
            res.push_back(atoi(input.c_str()));
        dp[input] = res;
        
        return res;
    }
};
原文地址:https://www.cnblogs.com/heyn1/p/11116227.html