[leetcode 241]Different Ways to Add Parentheses

来源:互联网 发布:linux samba进程 编辑:程序博客网 时间:2024/05/16 19:39

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、将输入字符串按照数字和运算符号分离,按顺序分别存入两个vector;

2、使用二维的vector作为标记,vector[i][j]表示字符串i到j的运算结果,因为运算结果可能有多个还有可能重复所以使用multiset

3、动态的计算vector[i][j],知道得到结果vector[0][input.size()-1];

AC代码:

class Solution{public:    vector<int> diffWaysToCompute(string input)    {        int sum=0;        vector<int> num;        vector<char> sign;        int len=input.size();        int x=0;        int y=0;        vector<int> res;        while(x<len)        {            if(input[x]>='0'&&input[x]<='9')            {                y=0;                while(x<len&&input[x]>='0'&&input[x]<='9')                {                    y=y*10+input[x]-48;                    ++x;                }                num.push_back(y);                ++sum;            }            else if(input[x]==' ')                ++x;            else            {                sign.push_back(input[x]);                ++x;            }        }        vector<vector<multiset<int> > > temp;        multiset<int> temp_set;        vector<multiset<int> >temp_vec;        for(int i=0; i<sum; ++i)            temp_vec.push_back(temp_set);        for(int i=0; i<sum; ++i)            temp.push_back(temp_vec);        for(int i=0; i<sum; ++i)            temp[i][i].insert(num[i]);        for(int i=1; i<sum; ++i)        {            if(sign[i-1]=='+')                temp[i-1][i].insert(num[i-1]+num[i]);            else if(sign[i-1]=='-')                temp[i-1][i].insert(num[i-1]-num[i]);            else                temp[i-1][i].insert(num[i-1]*num[i]);        }        for(int i=2; i<sum; ++i)        {            for(int j=0; j+i<sum; ++j)            {                multiset<int>::iterator ite=temp[j][j].begin();                for(multiset<int>::iterator ite2=temp[j+1][i+j].begin(); ite2!=temp[j+1][i+j].end(); ++ite2)                {                    if(sign[j]=='+')                        temp[j][i+j].insert(*ite+(*ite2));                    else if(sign[j]=='-')                        temp[j][i+j].insert(*ite-(*ite2));                    else                        temp[j][i+j].insert(*ite*(*ite2));                    }                ite=temp[i+j][i+j].begin();                for(multiset<int>::iterator ite2=temp[j][i+j-1].begin(); ite2!=temp[j][i+j-1].end(); ++ite2)                {                    if(sign[i+j-1]=='+')                        temp[j][i+j].insert(*ite2+(*ite));                    else if(sign[j+i-1]=='-')                        temp[j][i+j].insert(*ite2-(*ite));                    else                        temp[j][i+j].insert(*ite2*(*ite));                    }                for(int k=j+1; k<i+j-1; ++k)                {                    for(multiset<int>::iterator ite2=temp[j][k].begin(); ite2!=temp[j][k].end(); ++ite2)                    {                        for(multiset<int>::iterator ite3=temp[k+1][j+i].begin(); ite3!=temp[k+1][j+i].end(); ++ite3)                        {                            if(sign[k]=='+')                                temp[j][i+j].insert(*ite2+(*ite3));                            else if(sign[k]=='-')                                temp[j][i+j].insert(*ite2-(*ite3));                            else                                temp[j][i+j].insert(*ite2*(*ite3));                            }                    }                }            }        }        for(multiset<int>::iterator ite2=temp[0][sum-1].begin();ite2!=temp[0][sum-1].end();++ite2)            res.push_back(*ite2);        return res;    }};

其他Leetcode题目AC代码:https://github.com/PoughER/leetcode

0 0
原创粉丝点击