给出一个整数n。我们可以恰好翻转一位。编写代码以查找可以创建的最长1 s序列的长度。
例子:
Input : 1775
Output : 8
Binary representation of 1775 is 11011101111.
After flipping the highlighted bit, we get
consecutive 8 bits. 11011111111.
Input : 12
Output : 3
Input : 15
Output : 5
Input : 71
Output: 4
Binary representation of 71 is 1000111.
After flipping the highlighted bit, we get
consecutive 4 bits. 1001111.
一种简单的解决方案是将给定数字的二进制表示形式存储在二进制数组中。一旦将元素包含在二进制数组中,就可以应用此处讨论的方法。
一种有效的解决方案是遍历给定数字的二进制表示形式的位。我们跟踪当前1的序列长度和前一个1的序列长度。当我们看到一个零时,更新以前的长度:
- 如果下一位为1,则先前的长度应设置为当前长度。
- 如果下一位为0,则无法将这些序列合并在一起。因此,将上一个“长度”设置为0。
我们通过比较以下两个来更新最大长度:
- 最大长度的当前值
- 当前长度+上一个长度。
。
下面是上述想法的实现:
C++
// C++ program to find maximum consecutive
// 1's in binary representation of a number
// after flipping one bit.
#include
using namespace std;
int flipBit(unsigned a)
{
/* If all bits are l, binary representation
of 'a' has all 1s */
if (~a == 0)
return 8*sizeof(int);
int currLen = 0, prevLen = 0, maxLen = 0;
while (a!= 0)
{
// If Current bit is a 1 then increment currLen++
if ((a & 1) == 1)
currLen++;
// If Current bit is a 0 then check next bit of a
else if ((a & 1) == 0)
{
/* Update prevLen to 0 (if next bit is 0)
or currLen (if next bit is 1). */
prevLen = (a & 2) == 0? 0 : currLen;
// If two consecutively bits are 0
// then currLen also will be 0.
currLen = 0;
}
// Update maxLen if required
maxLen = max(prevLen + currLen, maxLen);
// Remove last bit (Right shift)
a >>= 1;
}
// We can always have a sequence of
// at least one 1, this is fliped bit
return maxLen+1;
}
// Driver code
int main()
{
// input 1
cout << flipBit(13);
cout << endl;
// input 2
cout << flipBit(1775);
cout << endl;
// input 3
cout << flipBit(15);
return 0;
}
Java
// Java program to find maximum consecutive
// 1's in binary representation of a number
// after flipping one bit.
class GFG
{
static int flipBit(int a)
{
/* If all bits are l, binary representation
of 'a' has all 1s */
if (~a == 0)
{
return 8 * sizeof();
}
int currLen = 0, prevLen = 0, maxLen = 0;
while (a != 0)
{
// If Current bit is a 1
// then increment currLen++
if ((a & 1) == 1)
{
currLen++;
}
// If Current bit is a 0 then
// check next bit of a
else if ((a & 1) == 0)
{
/* Update prevLen to 0 (if next bit is 0)
or currLen (if next bit is 1). */
prevLen = (a & 2) == 0 ? 0 : currLen;
// If two consecutively bits are 0
// then currLen also will be 0.
currLen = 0;
}
// Update maxLen if required
maxLen = Math.max(prevLen + currLen, maxLen);
// Remove last bit (Right shift)
a >>= 1;
}
// We can always have a sequence of
// at least one 1, this is fliped bit
return maxLen + 1;
}
static byte sizeof()
{
byte sizeOfInteger = 8;
return sizeOfInteger;
}
// Driver code
public static void main(String[] args)
{
// input 1
System.out.println(flipBit(13));
// input 2
System.out.println(flipBit(1775));
// input 3
System.out.println(flipBit(15));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find maximum
# consecutive 1's in binary
# representation of a number
# after flipping one bit.
def flipBit(a):
# If all bits are l,
# binary representation
# of 'a' has all 1s
if (~a == 0):
return 8 * sizeof();
currLen = 0;
prevLen = 0;
maxLen = 0;
while (a > 0):
# If Current bit is a 1
# then increment currLen++
if ((a & 1) == 1):
currLen += 1;
# If Current bit is a 0
# then check next bit of a
elif ((a & 1) == 0):
# Update prevLen to 0
# (if next bit is 0)
# or currLen (if next
# bit is 1). */
prevLen = 0 if((a & 2) == 0) else currLen;
# If two consecutively bits
# are 0 then currLen also
# will be 0.
currLen = 0;
# Update maxLen if required
maxLen = max(prevLen + currLen, maxLen);
# Remove last bit (Right shift)
a >>= 1;
# We can always have a sequence
# of at least one 1, this is
# fliped bit
return maxLen + 1;
# Driver code
# input 1
print(flipBit(13));
# input 2
print(flipBit(1775));
# input 3
print(flipBit(15));
# This code is contributed by mits
C#
// C# program to find maximum consecutive
// 1's in binary representation of a number
// after flipping one bit.
using System;
class GFG
{
static int flipBit(int a)
{
/* If all bits are l, binary representation
of 'a' has all 1s */
if (~a == 0)
{
return 8 * sizeof(int);
}
int currLen = 0, prevLen = 0, maxLen = 0;
while (a != 0)
{
// If Current bit is a 1
// then increment currLen++
if ((a & 1) == 1)
{
currLen++;
}
// If Current bit is a 0 then
// check next bit of a
else if ((a & 1) == 0)
{
/* Update prevLen to 0 (if next bit is 0)
or currLen (if next bit is 1). */
prevLen = (a & 2) == 0 ? 0 : currLen;
// If two consecutively bits are 0
// then currLen also will be 0.
currLen = 0;
}
// Update maxLen if required
maxLen = Math.Max(prevLen + currLen, maxLen);
// Remove last bit (Right shift)
a >>= 1;
}
// We can always have a sequence of
// at least one 1, this is fliped bit
return maxLen + 1;
}
// Driver code
public static void Main(String[] args)
{
// input 1
Console.WriteLine(flipBit(13));
// input 2
Console.WriteLine(flipBit(1775));
// input 3
Console.WriteLine(flipBit(15));
}
}
// This code contributed by Rajput-Ji
PHP
>= 1;
}
// We can always have a sequence of
// at least one 1, this is fliped bit
return $maxLen+1;
}
// Driver code
// input 1
echo flipBit(13);
echo "\n";
// input 2
echo flipBit(1775);
echo "\n";
// input 3
echo flipBit(15);
// This code is contributed by aj_36
?>
输出 :
4
8
5