将以下内容翻译为中文:最小化删除0子串以从循环二进制字符串中删除所有0的出现
在这个问题中,我们需要从给定的二进制字符串中移除所有的零。同时,我们需要一次性移除连续的一对零,并计算移除的零对的总数。
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中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

电脑C盘发现有个inetpub文件夹占用极大的内存,这个inetpub是什么文件夹?可以直接删除吗?其实inetpub是IIS服务端的一个文件夹,IIS全称InternetInformationServices,也就互联网信息服务,是可以搭建网站、调试网站用的,如果不需要的话,可以将其卸载掉。 具体方法如下: 1、右键点击开始菜单,选择“程序和功能”。 2、打开后点击“启用或关闭Windows功能”。 3、在Windows功能列表中,取消勾选II

小红书笔记怎么删除?在小红书APP中是可以编辑笔记的,多数的用户不知道小红书笔记如何的删除,接下来就是小编为用户带来的小红书笔记删除方法图文教程,感兴趣的用户快来一起看看吧!小红书使用教程小红书笔记怎么删除1、首先打开小红书APP进入到主页面,选择右下角【我】进入到专区;2、之后在我的专区,点击下图所示的笔记页面,选择要删除的笔记;3、进入到笔记页面,右上角【三个点】;4、最后下方会展开功能栏,点击【删除】即可完成。

1、首先,拉黑再删除永久加不上是假的,拉黑删除后想要再加对方,只要对方同意即可。2、如果用户将某人拉黑,对方将无法向用户发送消息、查看用户的朋友圈以及与用户进行通话。3、拉黑并不意味着将对方从用户的微信联系人列表中删除。4、如果用户在拉黑后又将对方从用户的微信联系人列表中删除,那么在删除后是没有办法恢复的。5、如果用户想再次添加对方为好友,需要对方同意并重新添加用户。

日常使用电脑的过程中,可能会收到found.000文件丢失损坏的错误提示,这个found.000是什么文件夹?如果没有用的话,可以将其删除吗?既然有这么多人不认识这个文件,下面小编就来跟大家仔细说说found.000文件夹吧~ 一、found.000是什么文件夹 当电脑出现因非法关机导致的文件部分或全部丢失时,可以在位于系统分区中指定目录下找到名为“found.000”的特殊文件夹及其内部包含的以“.chk”为扩展名的文件。 这个“fo

小红书作为一家流行的社交电商平台,吸引了大量用户分享生活点滴和购物心得。有时候我们可能会不经意发布一些不合适的内容,这时候需要及时删除,这样可以更好地维护个人形象或者遵守平台规定。一、小红书发布怎么删除?1.登录小红书账号,进入个人主页。2.在个人主页下方,找到“我的创作”选项,点击进入。3.在“我的创作”页面,你可以看到所有发布的内容,包括笔记、视频等。4.找到需要删除的内容,点击右侧的“...”按钮。5.在弹出的菜单中,选择“删除”选项。6.确认删除后,该条内容将从你的个人主页和公共页面消失

最近有很多网友问小编,hiberfil.sys是什么文件?hiberfil.sys占用了大量的C盘空间可以删除吗?小编可以告诉大家hiberfil.sys文件是可以删除的。下面就来看看详细的内容。hiberfil.sys是Windows系统中的一个隐藏文件,也是系统休眠文件。通常存储在C盘根目录下,其大小与系统安装内存大小相当。这个文件在计算机休眠时被使用,其中包含了当前系统的内存数据,以便在恢复时快速恢复到之前的状态。由于其大小与内存容量相等,因此它可能会占用较大的硬盘空间。 hiber

微信的文件传输助手是每个用户都有的,一些用户将它当做成了备忘录,会记录一些东西。那么微信文件传输助手怎么彻底删除呢?下面就让小编为大家详细介绍一下吧。微信文件传输助手怎么彻底删除答案:【微信】-【长按文件传输助手】-【删除该聊天】。具体步骤:1、首先打开微信软件,进入到首页中后我们找到【文件传输助手】长按;2、然后会弹出标为未读、置顶该聊天、不显示该聊天、删除该聊天,在这里我们点击【删除该聊天】即可;

1、打开抖音app,点击界面底部的【消息】,点击需要删除的聊天对话入口。2、长按任意一条聊天记录,点击【多选】,勾选想要删除的聊天记录。3、点击右下角的【删除】按钮,在弹出的窗口中选择【确认删除】即可将这些记录永久删除。
