给定一个32位整数 num,你可以将一个数位从0变为1。请编写一个程序,找出你能够获得的最长的一串1的长度。
示例 1:
输入: num = 1775(11011101111)
输出: 8
示例 2:
输入: num = 7(0111)
输出: 4
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reverse-bits-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
int reverseBits(int num) {
if(num==0)
return 1;
int prevlen, prevEnd, maxlen = 0, count = 0, start=-1;
int curstart, curlen, curEnd;
map<int,pair<int,int>> continOne;//连续1,结束pos : {开始位置,个数}
for(int i = 0; i < 32; ++i)
{
if((num>>i)&1) //为1
{
if(start==-1)
start = i;
count++;
if(i == 31)
continOne[i] = make_pair(start,count);
}
else
{
if(count)
continOne[i-1] = make_pair(start,count);
count = 0;
start = -1;
}
}
auto it = continOne.begin();
prevlen = it->second.second;
prevEnd = it->first;
maxlen = prevlen+1;
it++;
for(; it != continOne.end(); ++it)
{
curEnd = it->first;
curstart = it->second.first;
curlen = it->second.second;
if(curstart - prevEnd == 2)
maxlen = max(maxlen, curlen+prevlen+1);
else
maxlen = max(maxlen, curlen+1);
prevlen = curlen;
prevEnd = curEnd;
}
return maxlen;
}
};
class Solution {
public:
int reverseBits(int num) {
int prevlen = 0, curlen = 0, maxlen = 0;
for(int i = 0; i < 32; ++i)
{
if((num>>i)&1) //为1
curlen++;
else
{
maxlen = max(maxlen, curlen+prevlen+1);
prevlen = curlen;
curlen = 0;
}
}
return maxlen;
}
};