在这个问题中,我们需要从给定的二进制字符串中移除所有的零。同时,我们需要一次性移除连续的一对零,并计算移除的零对的总数。
We can solve the problem by counting the number of pairs of consecutive zeros in the given string. In this tutorial, we will learn two different solutions to solve the problem.
问题陈述 − 我们给出了一个长度为N的循环二进制字符串str。我们需要找到从字符串中移除所有零所需的最小连续零的数量。
示例示例
Input – str = "0011001"
Output – 2
Explanation
我们可以一起删除str[0]和str[1]。之后,我们可以删除str[4]和str[5]。所以,我们需要删除2对连续的零。
Input – str = ‘0000000’
Output – 1
Explanation
我们可以一次性删除所有的零。
Input – str = ‘00110010’
Output – 2
Explanation
We can remove str[0], str[1], and str[7] together as the binary string is circular. Next, we can remove str[5] and str[6] together.
Approach 1
在这种方法中,我们将找到给定字符串中连续零对的总数,这将回答给定的问题。
Algorithm
步骤 1 - 将'cnt'变量初始化为零。
第二步 - 将'isOne'变量初始化为false值,以跟踪给定字符串中的数字1。
步骤 3 - 使用循环迭代字符串。在循环中,如果当前字符是 '0',则将 'cnt' 的值增加 1。
步骤 4 − 使用 while 循环进行迭代,直到我们继续找到下一个字符为 '0',并将 'I' 的值增加 1。
第五步 - 如果当前字符是'1',将'isOne'变量的值更改为true,表示字符串中至少包含一个'1'。
Step 6 − Once the iteration of the loop completes, If the value of ‘isOne’ is false, it means the string contains only zeros. Return 1 in such cases.
Step 7 − If the first and last characters are ‘0’, decrease the value of the ‘cnt’ by 1 as the string is circular.
Step 8 − Return the value of ‘cnt’.
Example
的中文翻译为:示例
#include <bits/stdc++.h> using namespace std; int countRemovels(string str, int N){ // to store the count of 0s int cnt = 0; bool isOne = false; // Iterate over the string for (int i = 0; i < N; i++){ // If the current character is 0, increment the count of 0s if (str[i] == '0'){ cnt++; // traverse the string until a 1 is found while (str[i] == '0'){ i++; } } else{ // If the current character is 1, then set isOne as true isOne = true; } } // If string contains only 0s, then return 1. if (!isOne) return 1; // If the first and last character is 0, then decrement the count, as the string is circular. if (str[0] == '0' && str[N - 1] == '0'){ cnt--; } // return cnt return cnt; } int main(){ string str = "0011001"; int N = str.size(); cout << "The total number of minimum substrings of consecutive zeros required to remove is - " << countRemovels(str, N); return 0; }
输出
The total number of minimum substrings of consecutive zeros required to remove is - 2<font face="sans-serif"><span style="font-size: 16px; background-color: rgb(255, 255, 255);">.</span></font></p><p>
空间复杂度 - O(1)
方法二
在这种方法中,我们将通过计算相邻元素的不同来计算所需的最小删除零子串数量,以便删除所有零。
Algorithm
Step 1 − Define the ‘cnt’ and ‘isOne’ variables, and initialize with 0 and false, respectively.
Step 2 − Use for loop to make N-1 iterations, where N is the length of the string.
Step 3 − In the loop, check if the current character is ‘0,’ and the next character is ‘1’, increase the value of ‘cnt’ by 1. Otherwise, change the value of the ‘isOne’ variable to true.
第4步 - 如果最后一个字符是'0',并且第一个字符是'1',则将'cnt'的值增加1。
步骤 5 - 如果 'isOne' 的值为 false,则返回 1。
第6步 - 返回‘cnt’变量的值。
Example
的中文翻译为:示例
#include <bits/stdc++.h> using namespace std; int countRemovels(string str, int N){ // to store the count of 0s int cnt = 0; // to check if there is at least one 1 bool isOne = false; // traverse the string for (int i = 0; i < N - 1; i++) { // if the current character is 0, the next is 1, then increment count by 1 if (str[i] == '0' && str[i + 1] == '1'){ cnt++; } else{ // if the current character is 1, then set isOne to true isOne = true; } } // for circular string, if the last character is 0 and the first is 1, then increment count by 1 if (str[N - 1] == '0' && str[0] == '1'){ cnt++; } // if there is no 1 in the string, then return 1 if (!isOne){ return 1; } return cnt; // return cnt } int main(){ string str = "0011001"; int N = str.size(); cout << "The total number of minimum substrings of consecutive zeros required to remove is - " << countRemovels(str, N); return 0; }
输出
The total number of minimum substrings of consecutive zeros required to remove is - 2
结论
我们已经看到了两种不同的解决方案来解决给定的问题。在第一种方法中,我们计算连续的零对的总数,在第二种方法中,我们计算不匹配的相邻字符的总数。
以上是将以下内容翻译为中文:最小化删除0子串以从循环二进制字符串中删除所有0的出现的详细内容。更多信息请关注PHP中文网其他相关文章!

C#和C 的学习曲线和开发者体验有显着差异。 1)C#的学习曲线较平缓,适合快速开发和企业级应用。 2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C#和C 在面向对象编程(OOP)中的实现方式和特性上有显着差异。 1)C#的类定义和语法更为简洁,支持如LINQ等高级特性。 2)C 提供更细粒度的控制,适用于系统编程和高性能需求。两者各有优势,选择应基于具体应用场景。

从XML转换到C 并进行数据操作可以通过以下步骤实现:1)使用tinyxml2库解析XML文件,2)将数据映射到C 的数据结构中,3)使用C 标准库如std::vector进行数据操作。通过这些步骤,可以高效地处理和操作从XML转换过来的数据。

C#使用自动垃圾回收机制,而C 采用手动内存管理。1.C#的垃圾回收器自动管理内存,减少内存泄漏风险,但可能导致性能下降。2.C 提供灵活的内存控制,适合需要精细管理的应用,但需谨慎处理以避免内存泄漏。

C 在现代编程中仍然具有重要相关性。1)高性能和硬件直接操作能力使其在游戏开发、嵌入式系统和高性能计算等领域占据首选地位。2)丰富的编程范式和现代特性如智能指针和模板编程增强了其灵活性和效率,尽管学习曲线陡峭,但其强大功能使其在今天的编程生态中依然重要。

C 学习者和开发者可以从StackOverflow、Reddit的r/cpp社区、Coursera和edX的课程、GitHub上的开源项目、专业咨询服务以及CppCon等会议中获得资源和支持。1.StackOverflow提供技术问题的解答;2.Reddit的r/cpp社区分享最新资讯;3.Coursera和edX提供正式的C 课程;4.GitHub上的开源项目如LLVM和Boost提升技能;5.专业咨询服务如JetBrains和Perforce提供技术支持;6.CppCon等会议有助于职业

C#适合需要高开发效率和跨平台支持的项目,而C 适用于需要高性能和底层控制的应用。1)C#简化开发,提供垃圾回收和丰富类库,适合企业级应用。2)C 允许直接内存操作,适用于游戏开发和高性能计算。

C 持续使用的理由包括其高性能、广泛应用和不断演进的特性。1)高效性能:通过直接操作内存和硬件,C 在系统编程和高性能计算中表现出色。2)广泛应用:在游戏开发、嵌入式系统等领域大放异彩。3)不断演进:自1983年发布以来,C 持续增加新特性,保持其竞争力。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器