problem
solution1:
class Solution { public: int binaryGap(int N) { int res = 0; vector<int> pos; int k = 0; while(N>0) { if(N&1==1) pos.push_back(k); k++; N >>=1;//errr... } for(int i=1; i<pos.size(); ++i) { res = max(res, pos[i]-pos[i-1]); } return res; } };
solution2:
class Solution { public: int binaryGap(int N) { int res = 0, last = -1, k = 0; while(N>0) { if(N&1==1) { if(last>=0) res = max(res, k-last); last = k; } N>>=1;//errr... k++; } return res; } };
参考
1. Leetcode_easy_868. Binary Gap;
2. Grandyang;
完