1. 程式人生 > >869. 重新排序得到 2 的冪

869. 重新排序得到 2 的冪

class Solution { public:     vector<long long>ans;     vector<long long>zidian;     const vector<char> weishu = { '2','4','8','6' };     bool judge = false;     char trans;     bool judgefor2(int s)     {         while (s%2==0)         {             s /= 2;         }         if (s == 1) return true;         return false;     }     bool match(string s)     {         string temp = s;         for (int i = 0; i < 4; i++)         {             auto pos = temp.find(weishu[i]);             if (pos==string::npos)             {                 continue;             }             temp.erase(pos,1);             trans = weishu[i];             perm(temp, temp.length(), 0);             if (judge)             {                 return true;             }             temp = s;         }         return false;     }     void perm(string s, int len, int step)     {         if (len == step)         {             s += trans;             if (judgefor2(stoll(s)))             {                 judge = true;             }             //cout << s << endl;         }         else         {             for (int i = step; i < len; i++)             {                 swap(s[i], s[step]);                 perm(s, len, step + 1);                 swap(s[i], s[step]);             }         }     }     bool reorderedPowerOf2(int N)      {         if (N==1)         {             return true;         }         //for (int i = 0; i < 27; i++)         //{         //    zidian.push_back(pow(2, i));         //}         int n = N;         string temp = to_string(n);         if (match(temp)) return true;         //cout << ans.size() << endl;         return false;     } };