Leetcode "strobogrammatic number III"

Source: Internet
Author: User

It can be solved based on the code from "strobogrammatic number II". The idea is pretty straight forward-binary search the boundaries.

classSolution {intGO_CNT (intNBOOLBinner) {        intRET =0; Switch(n) { Case 0: Ret=1;// ""             Break;  Case 1: Ret=3;//{"0", "1", "8"};             Break;  Case 2:                        if(binner) ret + =1;//"XX"RET + =4;//{"One", "a", "a", "" "};             Break; default:                    intprev = go_cnt (N-2,true); intFAC =0; if(binner) FAC + =1; FAC+=4; RET= FAC *prev;  Break; }        returnret; } Vector<string> Go (intNBOOLBinner) {Vector<string>ret; if(n = =0)returnret; if(n = =1) {Ret.push_back ("0"); Ret.push_back ("1"); Ret.push_back ("8"); }        Else if(n = =2)        {            if(Binner) {Ret.push_back ("xx"); } ret.push_back (" One"); Ret.push_back (" the"); Ret.push_back (" the"); Ret.push_back (" the"); }        Else{vector<string> prev = Go (N-2,true); if(Binner) { for(Auto &S:prev) Ret.push_back ('0'+ S +'0'); }             for(Auto &S:prev) Ret.push_back ("1"+ S +"1");  for(Auto &S:prev) Ret.push_back ("6"+ S +"9");  for(Auto &S:prev) Ret.push_back ("8"+ S +"8");  for(Auto &S:prev) Ret.push_back ("9"+ S +"6"); }        returnret; } Public:    intStrobogrammaticinrange (stringLowstringHigh ) {        intRET =0; intLENL = Low.length (), lenh =high.length (); if(Lenl > Lenh)return 0; // Complete         for(intj = lenl +1; J < Lenh; J + +) ret+ = Go_cnt (J,false); intCnt0 =0, Cnt1 =0; Auto R0= Go (LENL,false); Auto It0=Std::lower_bound (R0.begin (), R0.end (), low); Cnt0= R0.end ()-it0; if(Lenh! =lenl) {Auto R1= Go (lenh,false); Auto It1=Std::upper_bound (R1.begin (), R1.end (), high); Cnt1= It1-R1.begin (); RET+ = Cnt0 +Cnt1; }        Else{Auto It1=Std::upper_bound (R0.begin (), R0.end (), high); Cnt1= It1-R0.begin (); RET= cnt0 + Cnt1-r0.size (); }        returnret; }};
View Code

Of course, we can optimize the vector generation Part:we only need one part of it:)

Leetcode "strobogrammatic number III"

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.