给定一个32位整数 num
,你可以将一个数位从0变为1。请编写一个程序,找出你能够获得的最长的一串1的长度。
示例 1:
输入: num = 1775(110111011112)
输出: 8
示例 2:
输入: num = 7(01112)
输出: 4
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int reverseBits(int num) {
int ps = 0, cur = 0, res = 0;
for (int i = 0; i++ < 32; num >>= 1) {
if ((num & 1) == 1) ++cur;
else {
ps = cur;
cur = 0;
}
res = Math.max(ps + cur + 1, res);
}
return res == 33 ? 32 : res;
}
}
//leetcode submit region end(Prohibit modification and deletion)