LeetCode Algorithms 241. Different Ways to Add Parentheses

題目難度: Medium


原題描述:

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]


題目大意:

        給定一個包含數字和運算符的算術表達式,求通過在這個算術表達式添加括號所產生的全部可能的運算順序得到的結果,運算符只有+、- 和 *。上面兩個例子說得比較清楚,看完就基本懂題意了。


解題思路:

        這道題是通過分治的標籤來找到的,所以解題方法應該就是分治,但是我的第一想法並不是分治,而是區間DP,在這裏分享一下我的做法。其實做完後回想起來,感覺我區間DP的做法就是分治(遞歸,自上而下)的循環實現(自下而上),原理是一樣的。

      總的思路如下:設dp[i][j]爲從第i個數(從0開始)到第j個數構成的算術表達式的所有結果輸出,則題目要求的結果爲dp[0][len-1],其中len爲算術表達式中全部數的個數。狀態轉移方程如下:dp[i][j] = dp[i][k] op dp[k+1][j] 的所有結果(做笛卡爾積),k>=i 且 k<j,op爲在原來的算術表達式中第k個數(從0開始)後面跟的符號。

        程序的大致框架如下:程序的主體爲diffWaysToCompute函數。這個函數包含幾重for循環,最外層的for循環爲遍歷區間右端點和左端點的差d;裏面一層循環爲遍歷在給定區間差時所有可能的區間;再裏面一層循環爲遍歷在給定區間時將其分成兩個子區間的所有可能性;最裏面的兩層循環是用於計算在給定兩個子區間後這兩個子區間做運算的所有結果。按照這樣的順序計算是因爲每個區間的計算要用到其分成所有可能的兩個子區間的結果。

      此外,extractNumAndOp函數用於將輸入字符串的數字和運算符分別提取出來,calFormula函數用於計算表達式的值。


時間複雜度分析:

        設算術表達式包含n個數,則最外面三層循環每一層都是O(n)的複雜度,因此外面三層循環的複雜度爲O(n^3),裏面兩層循環爲給定兩個子區間做運算的所有結果,複雜度應該是O(n^2)。因此總的複雜度爲O(n^5)?


以下是代碼:

const int maxLen = 500;
vector<int> dp[maxLen][maxLen];

class Solution {
public:
    void init()
{
    for(int i=0 ; i<maxLen ; ++i){
        for(int j=0 ; j<maxLen ; ++j){
            dp[i][j].clear();
        }
    }
}

//將輸入字符串的數字和運算符分別提取出來
void extractNumAndOp(string input , vector<int> & number , vector<char> & op)
{
    int sum = 0;
    for(int i=0 ; input[i]!='\0' ; ++i){
        if(isdigit(input[i])){
            sum = sum*10+(input[i]-'0');
        }
        else{
            number.push_back(sum);
            sum = 0;
            op.push_back(input[i]);
        }
    }
    number.push_back(sum);
}

int calFormula(int x , int y , char op)
{
    int sum = 0;
    switch(op)
    {
    case '+':
        sum = x+y;
        break;
    case '-':
        sum = x-y;
        break;
    case '*':
        sum = x*y;
        break;
    default:
        break;
    }
    return sum;
}

vector<int> diffWaysToCompute(string input)
{
    init();
    vector<int> number;
    vector<char> op;
    extractNumAndOp(input,number,op);
    int len = number.size();

    for(int i=0 ; i<len ; ++i){
        dp[i][i].push_back(number[i]);
    }

    for(int d=1 ; d<len ; ++d){
        for(int i=0 ; i<len-d ; ++i){
            //計算dp[i,i+d]
            for(int j=i ; j<i+d ; ++j){
                //計算dp[i][j] op dp[j+1][i+d],笛卡爾運算
                for(int x=0 ; x<dp[i][j].size() ; ++ x){
                    for(int y=0 ; y<dp[j+1][i+d].size() ; ++y){
                        dp[i][i+d].push_back( calFormula(dp[i][j][x], dp[j+1][i+d][y], op[j]) );
                    }
                }
            }
        }
    }
    sort(dp[0][len-1].begin() , dp[0][len-1].end());
    return dp[0][len-1];
}
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章