241. Different Ways to Add Parentheses

来源:互联网 发布:贾似道 知乎 编辑:程序博客网 时间:2024/06/05 06:46

LeetCode

  • 题目地址:https://leetcode.com/problems/different-ways-to-add-parentheses/#/description
  • 问题描述&解题思路:给一个字符串,比如”2*3-4*5“,根据运算顺序的不同组合,可以得到以下结果:

    (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

    一开始没什么思路,看到答案用递归的方法,以符号作为分割,左边字符串返回一组int向量,右边也是,再两两匹配,做+,-,*(根据该分割的符号来做)。递归的终止条件是字符串没有运算符,这时候返回一个数。

    但是递归的方法,会计算很多重复的部分(但是这道题的测试集不大,用动态规划的记忆的方法,反而慢了一点),于是参考动态规划,用一个hash map来存储已经做过递归的结果,其中因为有两个数字,一个是字符串起始位置,一个是结束位置,于是做了一个转换begin*size + end,这样来做索引。

  • 递归代码

class Solution {public:    vector<int> diffWaysToCompute(string input) {        vector<int> result;        int size = input.size();        for (int i = 0; i < size; i++) {            char cur = input[i];            if (cur == '+' || cur == '-' || cur == '*') {                // Split input string into two parts and solve them recursively                vector<int> result1 = diffWaysToCompute(input.substr(0, i));                vector<int> result2 = diffWaysToCompute(input.substr(i+1));                for (auto n1 : result1) {                    for (auto n2 : result2) {                        if (cur == '+')                            result.push_back(n1 + n2);                        else if (cur == '-')                            result.push_back(n1 - n2);                        else                            result.push_back(n1 * n2);                        }                }            }        }        // if the input string contains only number        if (result.empty())            result.push_back(atoi(input.c_str()));        return result;    }};
  • dp代码:
class Solution {public:    vector<int> diffWaysToCompute(string input) {        unordered_map<int,vector<int>> m;        return dp(input,0,input.size(),m);    }    //e: from 0 to size - 1    int trans(int b, int e, const int& size) {        return b * size + e;    }    //from b to e-1    vector<int> dp(const string& input, int b, int e, unordered_map<int,vector<int>> m) {        vector<int> res;        int size = input.size();        for (int i = b; i < e; i++) {            //每有一个运算符,就做一次            if (input[i] == '+' || input[i] == '-' || input[i] == '*') {                int l = trans(b,i-1,size);                int r = trans(i+1,e-1,size);                vector<int> r1,r2;                //left                if (m.count(l) == 0) {                    m[l] = dp(input,b,i,m);                }                r1 = m[l];                //right                if (m.count(r) == 0) {                    m[r] = dp(input,i+1,e,m);                }                r2 = m[r];                for (auto& a : r1) {                    for (auto& b : r2) {                        if (input[i] == '+') {                            res.push_back(a+b);                        }                        else if (input[i] == '-') {                            res.push_back(a-b);                        }                        else if (input[i] == '*') {                            res.push_back(a*b);                        }                    }                }            }        }        //如果res是空的,那么只有数字没有运算符        if (res.empty()) {            res.push_back(atoi(input.substr(b,e-b).c_str()));        }        return res;    }};
原创粉丝点击