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"