leetCode刷题归纳-Divide and Conquer(241. Different Ways to Add Parentheses)

Divide的题目相比起来难度会比较大,主要是分治的思想比较难以与实际应用结合起来,下面是题目描述:

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 *.

Example 1
Input: “2-1-1”.
((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]

最初的考虑


主要就是对标点符号的作用域要进行Divide,然后对于划分的每一个区域进行Conquer(计算),对输入的string进行扫描,观察到只有当扫描到运算符的时候才需要进行处理,对预算符的左右两部分进行分治运算,以题目中的例子为例画出处理的流程图:

这里写图片描述

需要注意两个方面的问题:

  • 跳出递归的条件设置:当被分治的对象为不包含运算符的字符串时,将字符串转化为int类型跳出,代码表现为:
 if (result.empty())
            result.push_back(atoi(input.c_str()));
  • 被分治的左右两部分(merge1 +/-/* merge2)可能存在多个可能值,利用循环计算出所有可能的值,表现在代码上:
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); 
}

将以上的思想归纳为代码如下:

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;
    }
};    

算法改进


以上算法有一个比较大的问题,就是进行了很多重复的运算,以输入为“2*3-4*5”为例,如果按照上面的思路,如下图所示,可以发现4*5运算做了两次,在输入的字符串规模很大的情况下,将浪费很多计算时间:

这里写图片描述

改进的想法是利用效率很高的unorder_map将之前计算过的字符串存储起来(典型的空间换时间的思路),实际上对于代码而言改动并不大:

class Solution {
public:
    vector<int> diffWaysToCompute(string input) {
        unordered_map<string,vector<int>>dpMap;
        return DpCompute(input,dpMap);
    }
    vector<int> DpCompute(string s,unordered_map<string,vector<int>>&dpMap){
        vector<int>res;
  //主要的改动体现在这里,如果
        if (dpMap.find(s)!=dpMap.end()) return dpMap[s];
        for(int i=0;i<s.size();i++){
            char cur=s[i];
            if (cur == '+' || cur == '-' || cur == '*'){
                vector<int>res1=DpCompute(s.substr(0,i),dpMap);
                vector<int>res2=DpCompute(s.substr(i+1),dpMap);

            for(int x:res1){
                for(int y:res2){
                    if (cur == '+' )  res.push_back(x+y);
                    else if (cur == '-' ) res.push_back(x-y);
                    else res.push_back(x*y);
                }
            }
        }
        }
        if(res.empty()) res.push_back(stoi(s));
        //将每一次计算过的string可能的值映射到数组中vector<int>
        dpMap[s]=res;
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值