The idea was similar to strobogrammatic number ii:generate all those in-range strobogrammatic numbers and count.
Refer to this link for a very readable code, which I has rewritten in C + + below.
1 classSolution {2 Public:3 solution () {4MP = {{'0','0'}, {'1','1'}, {'6','9'}, {'8','8'}, {'9','6'}};5 }6 7 intStrobogrammaticinrange (stringLowstringHigh ) {8 intAns =0, L = low.length (), u =high.length ();9 for(inti = l; I <= u; i++) {Ten stringTemp (I,' '); OneStrobogrammaticcount (temp, ans, low, high,0I1); A } - returnans; - } the Private: -unordered_map<Char,Char>MP; - voidStrobogrammaticcount (stringTempint& ans,string& Low,string& High,intLointhi) { - if(Lo >hi) { + if((temp[0] !='0'|| Temp.length () = =1) && less (low, temp) &&Less (temp, high)) -ans++; + return; A } at for(Auto m:mp) { -Temp[lo] =M.first; -Temp[hi] =M.second; - if(lo = Hi && m.first = = m.second) | | Lo <hi) -Strobogrammaticcount (temp, ans, low, high, lo +1, Hi-1); - } in } - BOOLLessstring& S,string&t) { to intm = s.length (), n =t.length (), I; + if(M! = N)returnM <N; - for(i =0; I < m; i++) the if(S[i] = = T[i])Continue; * Else Break; $ returni = = m | | S[i] <T[i];Panax Notoginseng } -};
[Leetcode] Strobogrammatic number III