A Natural DFS thought. Several pruning tricks can be applied. Please take care of date type (long long).
classSolution {BOOL_check (stringAstringBstringc) {intLenc =c.length (); if(!LENC)return true; if(Lenc >1&& c[0] =='0')return false; intLena =a.length (); intLenB =b.length (); if(Lenc < Max (Lena, LenB) | | (Lena >1&& a[0] =='0') || (LenB >1&& b[0] =='0'))return false; Long LongVA =Stoll (a); Long LongVB =Stoll (b); stringSsum = to_string (VA +vb); intLENS =ssum.length (); if(Lens > C.length ())return false; stringPre = C.SUBSTR (0, Lens); if(Pre! = ssum)return false; stringRest =c.substr (lens); if(Rest.length () >0&& rest[0] =='0')return false; return_check (b, ssum, rest); } Public: BOOLIsadditivenumber (stringnum) { intn =num.length (); for(inti =1; I <= N/2; i + +)//Lena { stringA = Num.substr (0, i); for(intj =1; J <= N-2I J + +)//LenB { stringb =Num.substr (i, j); stringc = num.substr (i +j); if(_check (A, B, c)) {return true; } } } return false; }};
Leetcode "Additive number"