LeetCode 282. Expression Add Operators(DFS)
题意:给你一个只有0-9组成的字符串,让你在其中插入'+','-','*'
使其计算得到的值恰好等于target,输出所有的可能性。
题解:不给数据范围,很显然就会考虑到超时,每个字符后面有四种选择,如果暴搜的话,字符长度超过10几个,就很快超时了。
但是我可以告诉你,这题目的字符串最多只有10个,所以不用考虑暴搜超时。这道题目考的是,你在暴搜到最后,计算这个表达式,会超时。
所以考你的就是在暴搜过程中,把表达式的值给计算出来。我个人而言,不喜欢这种不给数据范围,然后又不是正统套路的题目,。
class Solution {
public:
vector<string> ans;
int target2;
vector<string> addOperators(string num, int target) {
if(num.length()==0)
return ans;
target2 = target;
string s="";
s+=num[0];
fun(num,s,0,-1,-1,num[0]-'0','.','.');
return ans;
}
void fun(string num,string res,int pos,int last,int pre,int root,char operate,char operate2)
{
if(pos==num.length()-1)
{
if(operate!='.'&&operate2!='.')
{
if(operate2=='*')
{
if(compute(root,compute(pre,last,'*'),operate)==target2)
{
ans.push_back(res);
}
}
else
{
if(compute(compute(root,pre,operate),last,operate2)==target2)
{
ans.push_back(res);
}
}
}
else if(operate!='.'&&operate2=='.')
{
if(compute(root,pre,operate)==target2)
ans.push_back(res);
}
else
{
if(root==target2)
ans.push_back(res);
}
return;
}
if(operate=='.')
{
if(root!=0&&((long long int)root)*10+(long long int)(num[pos+1]-'0') <= INT_MAX)
{
fun(num,res+num[pos+1],pos+1,-1,-1,root*10+(num[pos+1]-'0'),'.','.');
}
fun(num,res+"+"+num[pos+1],pos+1,-1,num[pos+1]-'0',root,'+','.');
fun(num,res+"-"+num[pos+1],pos+1,-1,num[pos+1]-'0',root,'-','.');
fun(num,res+"*"+num[pos+1],pos+1,-1,num[pos+1]-'0',root,'*','.');
}
else if(operate!='.'&&operate2=='.')
{
if(pre!=0&&((long long int)pre)*10+(long long int)(num[pos+1]-'0') <= INT_MAX)
{
fun(num,res+num[pos+1],pos+1,-1,pre*10+(num[pos+1]-'0'),root,operate,'.');
}
fun(num,res+"+"+num[pos+1],pos+1,num[pos+1]-'0',pre,root,operate,'+');
fun(num,res+"-"+num[pos+1],pos+1,num[pos+1]-'0',pre,root,operate,'-');
fun(num,res+"*"+num[pos+1],pos+1,num[pos+1]-'0',pre,root,operate,'*');
}
else
{
if(last!=0&&((long long int)last)*10+(long long int)(num[pos+1]-'0') <= INT_MAX)
{
fun(num,res+num[pos+1],pos+1,last*10+(num[pos+1]-'0'),pre,root,operate,operate2);
}
if(operate=='*')
{
fun(num,res+"+"+num[pos+1],pos+1,num[pos+1]-'0',last,root*pre,operate2,'+');
fun(num,res+"-"+num[pos+1],pos+1,num[pos+1]-'0',last,root*pre,operate2,'-');
fun(num,res+"*"+num[pos+1],pos+1,num[pos+1]-'0',last,root*pre,operate2,'*');
}
else if(operate2=='*')
{
fun(num,res+"+"+num[pos+1],pos+1,num[pos+1]-'0',pre*last,root,operate,'+');
fun(num,res+"-"+num[pos+1],pos+1,num[pos+1]-'0',pre*last,root,operate,'-');
fun(num,res+"*"+num[pos+1],pos+1,num[pos+1]-'0',pre*last,root,operate,'*');
}
else
{
fun(num,res+"+"+num[pos+1],pos+1,num[pos+1]-'0',last,operate=='+'?root+pre:root-pre,operate2,'+');
fun(num,res+"-"+num[pos+1],pos+1,num[pos+1]-'0',last,operate=='+'?root+pre:root-pre,operate2,'-');
fun(num,res+"*"+num[pos+1],pos+1,num[pos+1]-'0',last,operate=='+'?root+pre:root-pre,operate2,'*');
}
}
}
int compute(int x,int y,char operate)
{
if(operate=='+')
return x+y;
else if(operate=='-')
return x-y;
else
{
if((long long int)x*(long long int)y>INT_MAX)
return -1;
return x*y;
}
}
};