zoukankan      html  css  js  c++  java
  • LeetCode 282. Expression Add Operators (Hard,递归分治)

    题目

    题解:
    如标题,其实就是暴搜啦

    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;
            }
        }
        
    };
    
  • 相关阅读:
    HDU ACM 1071 The area 定积分计算
    Effective C++:条款25:考虑写出一个不抛异常的swap函数
    史上最全站点降权原因解析
    shell脚本中的数学运算
    Spark 1.0.0 横空出世 Spark on Yarn 部署(Hadoop 2.4)
    索尼 LT26I刷机包 X.I.D 增加官方风格 GF A3.9.4 各方面完美
    Swift基础--使用TableViewController自己定义列表
    勒索软件出新招,小心你的隐私和財产安全!
    Http协议具体解释
    Android Studio解决unspecified on project app resolves to an APK archive which is not supported
  • 原文地址:https://www.cnblogs.com/dacc123/p/12883618.html
Copyright © 2011-2022 走看看