将给定二进制字符串中的所有 0 翻转 K 次,且相邻字符不同
在考虑相邻字符的情况下翻转二进制字符串中的 0 的任务在各个领域都有实际应用。在本教程中,我们将深入探讨修改给定二进制字符串的问题,方法是重复翻转具有不同相邻字符的 0。具体来说,我们的目标是在 C++ 编程的上下文中解决这个问题。
该解决方案涉及迭代扫描字符串并根据提供的逻辑应用必要的翻转。通过利用字符串操作功能,我们可以有效地通过翻转 K 次 0 来转换二进制字符串,确保每次翻转都符合具有不同邻居的标准。通过对问题陈述的深入检查,并辅以 C、C++、Java 和 Python 的逐步实现,本教程提供了解决这个有趣的二进制字符串操作挑战的综合指南。让我们开始吧!
问题陈述
给定一个二进制字符串和翻转次数,任务是翻转字符串中所有出现的“0”,同时考虑相邻字符。目标是将“0”更改为“1”,并且如果相邻字符为“1”,则也将它们翻转为“0”。
示例 1
输入
binary string: "01001"; Number of flips: 4
输出
Flipped string: 00011
解释:初始二进制字符串为“01001”。应用翻转后,所有“0”都转换为“1”。如果相邻字符为“1”,则也会翻转它们。生成的翻转字符串为“00011”。
示例 2
输入
binary string: 101010; Number of flips: 2
输出
Flipped string: 010110
解释 − 在这种情况下,初始二进制字符串为“101010”。由于字符串中没有“0”,因此无法应用翻转。因此,生成的翻转字符串与输入字符串相同,即“010110”。
算法
首先定义 `flipZeroes` 函数,该函数以二进制字符串和翻转次数作为输入。
创建二进制字符串的副本,并将其存储在 `result` 变量中。
确定二进制字符串的长度,并将其存储在变量 `n` 中。
使用循环迭代字符串,从索引 0 开始,直到到达字符串末尾或翻转次数变为 0。
检查当前字符是否为“0”。
如果是,则通过将“1”赋值给 `result` 字符串中的当前索引来将“0”翻转为“1”。
检查相邻字符(如果有)并在它们为“1”时翻转它们。
将剩余翻转次数减 1。
返回生成的翻转字符串。
在主函数中,提供示例二进制字符串和翻转次数。
使用二进制字符串和翻转次数作为参数调用 `flipZeroes` 函数。
输出原始二进制字符串、翻转次数和生成的翻转字符串。
总的来说,该程序允许翻转二进制字符串中的“0”,同时考虑相邻字符,并显示原始字符串、翻转次数和生成的翻转字符串的输出。
示例
以上算法在各种编程语言中的实现
下面的程序将二进制字符串作为输入,并在字符串上执行指定的翻转次数。`flipZeroes` 函数迭代字符串,将“0”翻转为“1”,并检查相邻字符以在可能的情况下翻转它们。`flips` 变量确定要执行的最大翻转次数。然后,程序输出原始二进制字符串、翻转次数和生成的翻转字符串。
注意 − 程序假设输入字符串仅包含 0 和 1。如果输入字符串包含其他字符,则程序可能无法按预期工作。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to flip '0's in a binary string
char* flipZeroes(const char* binaryString, int flips) {
char* result = strdup(binaryString);
int n = strlen(binaryString);
// Loop through the string
for (int i = 0; i < n && flips > 0; ++i) {
// Check if the current character is '0'
if (result[i] == '0') {
// Flip the '0' to '1'
result[i] = '1';
// Check the neighbors and flip them if possible
if (i - 1 >= 0 && result[i - 1] == '1') {
result[i - 1] = '0';
} else if (i + 1 < n && result[i + 1] == '1') {
result[i + 1] = '0';
}
--flips; // Decrease the number of remaining flips
}
}
return result;
}
int main() {
const char* binaryString = "01001";
int flips = 4;
printf("Input binary string: %s\n", binaryString);
printf("Number of flips: %d\n", flips);
char* result = flipZeroes(binaryString, flips);
printf("Flipped string: %s\n", result);
free(result); // Don't forget to free the allocated memory
return 0;
}
输出
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
#include <iostream>
#include <string>
std::string flipZeroes(const std::string& binaryString, int flips) {
std::string result = binaryString;
int n = binaryString.length();
// Loop through the string
for (int i = 0; i < n && flips > 0; ++i) {
// Verify whether the current character is equal to '0'
if (result[i] == '0') {
// Change the current '0' to '1' by flipping it
result[i] = '1';
// Check the neighbours and flip them if possible
if (i - 1 >= 0 && result[i - 1] == '1') {
result[i - 1] = '0';
} else if (i + 1 < n && result[i + 1] == '1') {
result[i + 1] = '0';
}
--flips; // Decrease the number of remaining flips
}
}
return result;
}
int main() {
std::string binaryString = "01001";
int flips = 4;
std::cout << "Input binary string: " << binaryString << std::endl;
std::cout << "Number of flips: " << flips << std::endl;
std::string result = flipZeroes(binaryString, flips);
std::cout << "Flipped string: " << result << std::endl;
return 0;
}
输出
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
public class BinaryStringFlip {
// Function to flip '0's in a binary string
public static String flipZeroes(String binaryString, int flips) {
char[] result = binaryString.toCharArray();
int n = binaryString.length();
int i = 0;
while (i < n && flips > 0) {
if (result[i] == '0') {
result[i] = '1';
if (i - 1 >= 0 && result[i - 1] == '1') {
result[i - 1] = '0';
} else if (i + 1 < n && result[i + 1] == '1') {
result[i + 1] = '0';
}
flips--;
}
i++;
}
return new String(result);
}
public static void main(String[] args) {
String binaryString = "01001";
int flips = 4;
System.out.println("Input binary string: " + binaryString);
System.out.println("Number of flips: " + flips);
String result = flipZeroes(binaryString, flips);
System.out.println("Flipped string: " + result);
}
}
输出
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
def flip_zeroes(binary_string, flips):
result = list(binary_string)
n = len(binary_string)
i = 0
while i < n and flips > 0:
if result[i] == '0':
result[i] = '1'
if i - 1 >= 0 and result[i - 1] == '1':
result[i - 1] = '0'
elif i + 1 < n and result[i + 1] == '1':
result[i + 1] = '0'
flips -= 1
i += 1
return ''.join(result)
binary_string = "01001"
flips = 4
print("Input binary string:", binary_string)
print("Number of flips:", flips)
result = flip_zeroes(binary_string, flips)
print("Flipped string:", result)
输出
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
注意 − 对于不同的输入字符串和 K 值,输出可能不同。
结论
总而言之,使用字符串操作的强大功能可以有效地解决在考虑相邻字符的情况下将二进制字符串中的 0 翻转 K 次的问题。通过遵循提供的逻辑并迭代字符串,我们可以根据其相邻字符识别需要翻转的 0。本教程中讨论的实现展示了一种清晰简洁的方法来解决这个问题,提供了一种可应用于现实世界场景的实用解决方案。无论是数据处理、算法问题解决还是其他相关任务,能够有效地通过翻转具有不同邻居的 0 来修改二进制字符串都是一项宝贵的技能。通过理解问题陈述并利用各种编程语言的功能,读者可以自信地应对类似的挑战并扩展其编程专业知识。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP