[leetcode]241. 为运算表达式设计优先级

在这里插入图片描述

1、提交的代码,分治

class Solution {
    map< pair<int,int>, vector<int> >hash;  // 备忘录  key: <left,right>
public:
    vector<int> ways(const string &input, int left, int right)
    {
        pair<int,int>curKey(left, right);
        if(hash.find(curKey) != hash.end())
        {//之前存了
            return hash[curKey];
        }
        vector<int> res;
        for(int i = left; i < right; i++)
        {
            if(input[i] == '-' || input[i] == '+' || input[i] == '*')
            { //遇到操作符分别递归计算左边、右边,
              //当前选择的操作符优先最低(递归的下一层的表达式先算)
                vector<int>left_result = ways(input, left, i);
                vector<int>right_result = ways(input, i+1, right);

                for(auto l: left_result)
                {
                    for(auto r:right_result)
                    {
                        res.push_back(cal(input[i], l, r));
                    }
                }
            }
        }
        if(res.size() == 0)
        {
            res.push_back(stoi(input.substr(left, right - left)));
        }
        hash[curKey] = res;
        return res;
    }
    int cal(char ops, int a, int b)
    {
        int res;
        switch(ops)
        {
            case '-': res = a - b; break;
            case '+': res = a + b; break;
            case '*': res = a * b; break;
        }
        return res;
    }
    vector<int> diffWaysToCompute(string input) {
        return ways(input, 0, input.size());
    }
};

2、在自己电脑上打印输出:

#include <bits/stdc++.h>
using namespace std;
 
struct Node
{
    int num;
    string exp;
    Node(int n,string e):num(n),exp(e){}
};

class Solution {
    map< pair<int,int>, vector<Node> >hash;  // 备忘录  key: <left,right>
public:
    vector<Node> ways(const string &input, int left, int right)
    {
        pair<int,int>curKey(left, right);
        if(hash.find(curKey) != hash.end())
        {//之前存了
            return hash[curKey];
        }
        vector<Node> res;
        for(int i = left; i < right; i++)
        {
            if(input[i] == '-' || input[i] == '+' || input[i] == '*')
            { //遇到操作符分别递归计算左边、右边,
              //当前选择的操作符优先最低(递归的下一层的表达式先算)
                vector<Node>left_result = ways(input, left, i);
                vector<Node>right_result = ways(input, i+1, right);

                for(int ii = 0; ii < left_result.size(); ii++)
                {
                    for(int jj = 0; jj < right_result.size(); jj++)
                    {
                        int num = cal(input[i], left_result[ii].num, right_result[jj].num);
                        string exp = "(" + left_result[ii].exp + " " + input[i] + " "+ right_result[jj].exp + ")";
                        res.push_back(Node(num, exp));
                    }
                }
            }
        }
        if(res.size() == 0)
        {//我的编译器用不了C++ 11, 所以用不了stoi,这里用int atoi(const char *str);
		//c_str(): string -> char *str 
            int num = atoi(input.substr(left, right - left).c_str());
            string exp = input.substr(left, right - left);
			res.push_back(Node(num, exp)); 
        }
        hash[curKey] = res;
        return res;
    }
    int cal(char ops, int a, int b)
    {
        int res;
        switch(ops)
        {
            case '-': res = a - b; break;
            case '+': res = a + b; break;
            case '*': res = a * b; break;
        }
        return res;
    }
    vector<Node> diffWaysToCompute(string input) {
        return ways(input, 0, input.size());
    }
};
int main() 
{ 
    string input = "5*4-3*2"; 
    Solution s;
    vector<Node> res = s.diffWaysToCompute(input);  
  
    for (int i = 0; i < res.size(); i++) 
        cout <<res[i].exp<<" = "<< res[i].num<<endl; 
    return 0; 
} 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值