leetcode_c++:Divide and Conquer: Different Ways to Add Parentheses(241)

原创 2016年08月28日 15:39:05

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]

Example 2
Input: “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
Output: [-34, -14, -10, -10, 10]


#include <bits/stdc++.h>

using namespace std;
const int N = 110;

class Solution {
private:
    vector<int> dp[N][N];
    vector<int> helper(string& input, int l, int r) {
        if (!dp[l][r].empty())
            return dp[l][r];
        vector<int> &ans = dp[l][r];
        bool isNum = true;
        int num = 0;
        for (int i = l; i < r; ++i) {
            if (!isdigit(input[i])) {
                isNum = false;
                vector<int> L = helper(input, l, i), R = helper(input, i + 1, r);
                for (auto l : L)
                    for (auto r : R) {
                        if (input[i] == '+') ans.push_back(l + r);
                        else if (input[i] == '-') ans.push_back(l - r);
                        else ans.push_back(l * r);
                    }
            }
            if (isNum) 
                num = num * 10 + (input[i] - '0');
        }
        if (isNum)
            ans.push_back(num);
        return ans;
    }
public:
    vector<int> diffWaysToCompute(string input) {
        return helper(input, 0, input.length());
    }
};

int main() {
    Solution s;
    vector<int> ans;
    // ans = s.diffWaysToCompute("0+1");
    // for (auto i : ans) cout << i << ' ';
    // cout << endl;
    // ans = s.diffWaysToCompute("2-1-1");
    // for (auto i : ans) cout << i << ' ';
    // cout << endl;
    ans = s.diffWaysToCompute("2*3-4*5");
    for (auto i : ans) cout << i << ' ';
    cout << endl;
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[Divide and Conquer]241. Different Ways to Add Parentheses

题目: 题目分析: 1、根据题目要求,函数的输入为一个只涉及"+"、"-"、"×"三种运算的字符串。需要根据输入计算出所有可能计算顺序所对应的结果,并返回包含所有答案的Vector(升序)。 2、问...

Divide and Conquer (1) -- Kth Largest Element in an Array,Different Ways to Add Parentheses

Kth Largest Element in an Array,Different Ways to Add Parentheses
  • dwt0317
  • dwt0317
  • 2017年01月12日 22:59
  • 97

Leetcode divide & conquer || Different Ways to Add Parentheses

本题来源于Leetcode Divide and Conquer题库,难度Medium

【Leetcode】241. Different Ways to Add Parentheses

题目: Given a string of numbers and operators, return all possible results from computing all the dif...

leetcode 241. Different Ways to Add Parentheses-分治算法

原题链接:241. Different Ways to Add Parentheses 【思路-Java】 递归实现 采用分治算法,分治算法的基本思想是将一个规模为N的问题分解为K个规模较小的子问题,...

leetcode 241:Different Ways to Add Parentheses

递归计算字符串算术表达式

Leetcode 241. Different Ways to Add Parentheses

 题目: Given a string of numbers and operators, return all possible results from computing all the ...
  • Lau_r_k
  • Lau_r_k
  • 2017年03月04日 17:24
  • 176

[leetcode] 241.Different Ways to Add Parentheses

题目: Given a string of numbers and operators, return all possible results from computing all the dif...

LeetCode[241. Different Ways to Add Parentheses] 难度[medium]

分治算法以及menmorization优化

LeetCode 241. Different Ways to Add Parentheses(加括号的不同方法)

原题网址:https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers an...
  • jmspan
  • jmspan
  • 2016年04月07日 02:58
  • 335
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer: Different Ways to Add Parentheses(241)
举报原因:
原因补充:

(最多只允许输入30个字)