• 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;
            }
        }
        
    };
    
  • 相关阅读:
    Axure 8 注册码,市面上很多注册码都不行用,但是这个可以。
    SmartGit Mac、Liunx、Windows过期后破解方法
    iOS蓝牙4.0开发例子
    苹果发布【新开发语言】Swift
    iOS Web开发
    [置顶]JB开发之制作系统级Application
    mac 10.9 dock在多屏幕间移动
    IOS Application Security Testing Cheat Sheet
    看上去很美 国内CDN现状与美国对比
    iOS 企业证书发布app 流程
  • 原文地址:https://www.cnblogs.com/dacc123/p/12883618.html
Copyright © 2020-2023  润新知