Leetcode "Additive number"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.