给定字符串形式的二进制数,任务是打印一个二进制等效项,该二进制等效项是通过仅翻转一组连续的0获得的,从而使该二进制数的十进制等效项最大。
注意:不要假设二进制数字的开头有任何结尾的零,即“ 0101”指定为“ 101”。
例子:
Input: s = “10101”
Output: 11101
Explanation:
Here we can only flip the 2nd character of the string “10101” ( = 21) that will change it to “11101” (= 29). Since we are allowed to flip a continuous subarray, any more flipping will lead to decrease in decimal equivalent.
Input: s = “1000”
Output: 1111
Explanation:
If we flip the continuous characters starting from position 1 till 3 we will get 1111 which is the maximum number possible in 4 bits i.e. 15.
方法:要解决上述问题,我们知道必须增加二进制等效项的值。因此,我们必须在较高位置增加1的数量。显然,我们可以通过仅翻转最初出现的零来增加数字的值。遍历字符串并翻转第一次出现的零,直到出现1,在这种情况下,循环必须中断。打印结果字符串。
下面是上述方法的实现:
C++
// C++ implementation to Maximize the value of
// the decimal equivalent given in the binary form
#include
using namespace std;
// Function to print the binary number
void flip(string& s)
{
for (int i = 0; i < s.length(); i++) {
// Check if the current number is 0
if (s[i] == '0') {
// Find the continuous 0s
while (s[i] == '0') {
// Replace initially
// occurring 0 with 1
s[i] = '1';
i++;
}
// Break out of loop if 1 occurs
break;
}
}
}
// Driver code
int main()
{
string s = "100010001";
flip(s);
cout << s;
return 0;
}
Java
// Java implementation to maximize the value of
// the decimal equivalent given in the binary form
import java.util.*;
class GFG{
// Function to print the binary number
static void flip(String s)
{
StringBuilder sb = new StringBuilder(s);
for(int i = 0; i < sb.length(); i++)
{
// Check if the current number is 0
if (sb.charAt(i) == '0')
{
// Find the continuous 0s
while (sb.charAt(i) == '0')
{
// Replace initially
// occurring 0 with 1
sb.setCharAt(i, '1');
i++;
}
// Break out of loop if 1 occurs
break;
}
}
System.out.println(sb.toString());
}
// Driver code
public static void main(String[] args)
{
String s = "100010001";
flip(s);
}
}
// This code is contributed by offbeat
Python3
# Python3 implementation to
# Maximize the value of the
# decimal equivalent given
# in the binary form
# Function to print the binary
# number
def flip(s):
s = list(s)
for i in range(len(s)):
# Check if the current number
# is 0
if(s[i] == '0'):
# Find the continuous 0s
while(s[i] == '0'):
# Replace initially
# occurring 0 with 1
s[i] = '1'
i += 1
s = ''.join(map(str, s))
# return the string and
# break the loop
return s
# Driver code
s = "100010001"
print(flip(s))
# This code is contributed by avanitrachhadiya2155
C#
// C# implementation to maximize the value of
// the decimal equivalent given in the binary form
using System;
class GFG{
// Function to print the binary number
static String flip(char []s)
{
for(int i = 0; i < s.Length; i++)
{
// Check if the current number is 0
if (s[i] == '0')
{
// Find the continuous 0s
while (s[i] == '0')
{
// Replace initially
// occurring 0 with 1
s[i] = '1';
i++;
}
// Break out of loop if 1 occurs
break;
}
}
return new String(s);
}
// Driver code
public static void Main(String[] args)
{
String s = "100010001";
Console.WriteLine(flip(s.ToCharArray()));
}
}
// This code is contributed by Rohit_ranjan
111110001
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。