分治法学习之多项式运算顺序

LeetCode题目 241Different 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 *.

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]

分析

这道题可以用分治法来做。分治法有三个步骤。1、如何分成n个子问题;2、递归求解,定义原子问题;3、如何合并子问题。再看这道题,显而易见地看到输入的字符串包括k个数和k-1个运算符。然后要求我们根据不同的运算顺序得到不同的运算结果。所以第一个步骤我们确定了子问题就是将k-1个运算符拆成子问题。第二个步骤,递归求解。递归的思路是将运算符作为中间的断开点,左右两边分别递归下去。原子问题是当没有运算符的时候, 直接返回该数。第三个步骤就是将左右两边得到的数组相乘,得到的新数组就是所有可能的结果。

预先处理数据

在本题中,我不是直接用string作为递归的参数,而是先将string处理成一个整型数组和一个运算符的char数组。然后在将这两个数组分开作为子问题。

vector<int> diffWaysToCompute(string input) {
    vector<int> nums;
    vector<char> punc;
    string str;
    int num;
    for (int i = 0; i < input.length(); i++) {
        if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
            punc.push_back(input[i]);
            num = StrToInt(str);
            nums.push_back(num);
            num = 0;
            str = "";
        }
        else {
            str += input[i];
        }
    }
    num = StrToInt(str);
    nums.push_back(num);
    vector<int> result = ComputeVec(nums, punc);
    return result;
}

递归并确定原子问题

vector<int> ComputeVec(vector<int> nums, vector<char> punc) {
    vector<int> result;
    if (nums.size() != punc.size() + 1) return result;
    if (punc.size() == 0) result.push_back(nums[0]);
    else {
        for (int i = 0; i < punc.size(); i++) {
            vector<int> left = truncInt(nums, 0, i);
            vector<int> right = truncInt(nums, i + 1, nums.size() - 1);
            vector<char> leftpunc = truncChar(punc, 0, i - 1);
            vector<char> rightPunc = truncChar(punc, i + 1, punc.size()-1);
            vector<int> vec = ComputeTwoVec(ComputeVec(left, leftpunc), ComputeVec(right, rightPunc), punc[i]);
            for (int j = 0; j < vec.size(); j++) result.push_back(vec[j]);
        }
    }
    return result;
}

合并递归结果

vector<int> ComputeTwoVec(vector<int> left, vector<int> right, char c) {
    vector<int> result;
    for (int i = 0; i < left.size(); i++) {
        for (int j = 0; j < right.size(); j++) {
            result.push_back(Compute(left[i], right[j], c));
        }
    }
    return result;
}

完整代码

using namespace std;
class Solution {
public:

int StrToInt(string str) {
    int len = str.length();
    int i = 0, result = 0;
    while (i < len) {
        result = result * 10 + (str[i] - '0');
        i++;
    }
    return result;
}

int Compute(int a, int b, char c) {
    if (c == '+') return a + b;
    if (c == '-') return a - b;
    if (c == '*') return a*b;
    return 0;
}

vector<int> truncInt(vector<int> vec, int begin, int end) {
    vector<int> result;
    if (end < begin || end >= vec.size()) return result;
    for (int i = begin; i <= end; i++) {
        result.push_back(vec[i]);
    }
    return result;
}

vector<char> truncChar(vector<char> vec, int begin, int end) {
    vector<char> result;
    if (end < begin || end >= vec.size()) return result;
    for (int i = begin; i <= end; i++) {
        result.push_back(vec[i]);
    }
    return result;
}

vector<int> ComputeTwoVec(vector<int> left, vector<int> right, char c) {
    vector<int> result;
    for (int i = 0; i < left.size(); i++) {
        for (int j = 0; j < right.size(); j++) {
            result.push_back(Compute(left[i], right[j], c));
        }
    }
    return result;
}

vector<int> ComputeVec(vector<int> nums, vector<char> punc) {
    vector<int> result;
    if (nums.size() != punc.size() + 1) return result;
    if (punc.size() == 0) result.push_back(nums[0]);
    else {
        for (int i = 0; i < punc.size(); i++) {
            vector<int> left = truncInt(nums, 0, i);
            vector<int> right = truncInt(nums, i + 1, nums.size() - 1);
            vector<char> leftpunc = truncChar(punc, 0, i - 1);
            vector<char> rightPunc = truncChar(punc, i + 1, punc.size()-1);
            vector<int> vec = ComputeTwoVec(ComputeVec(left, leftpunc), ComputeVec(right, rightPunc), punc[i]);
            for (int j = 0; j < vec.size(); j++) result.push_back(vec[j]);
        }
    }
    return result;
}

vector<int> diffWaysToCompute(string input) {
    vector<int> nums;
    vector<char> punc;
    string str;
    int num;
    for (int i = 0; i < input.length(); i++) {
        if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
            punc.push_back(input[i]);
            num = StrToInt(str);
            nums.push_back(num);
            num = 0;
            str = "";
        }
        else {
            str += input[i];
        }
    }
    num = StrToInt(str);
    nums.push_back(num);
    vector<int> result = ComputeVec(nums, punc);
    return result;
}
};

参考答案

class Solution {
public:
    vector<int> diffWaysToCompute(string input) {
        vector<int> res;
        for (int i=0; iif (!isdigit(input[i])) {
                string front = input.substr(0, i);
                string back = input.substr(i+1);
                vector<int> frontR = diffWaysToCompute(front);
                vector<int> backR = diffWaysToCompute(back);

                for (int j=0; jfor (int k=0; kif (input[i]=='+') res.push_back(frontR[j]+backR[k]);
                        if (input[i]=='-') res.push_back(frontR[j]-backR[k]);
                        if (input[i]=='*') res.push_back(frontR[j]*backR[k]);                        
                    }
                }
            }
        }
        if (res.size()==0) res.push_back(atoi(input.c_str()));
        return res;
    }
};

思考

这道题看了参考答案之后,感觉思路相差不多,但是自己的步骤多了几倍。主要差别是,本题可以直接用string作为参数传递进去而并非转化成vector,并且vector没有很明显的根据位置截取数据,而是要自定义的一个函数来截取。此外,vector又规定了内容类型,这样对代码的可读性和空间复杂度影响很大。另外,stdlib.h库里面有一个直接将string转化为int的函数atoi,自己定义增加了代码量。

附录

题目地址: Different Ways to Add Parentheses

你可能感兴趣的:(LeetCode)