将以下内容翻译为中文:最小化删除0子串以从循环二进制字符串中删除所有0的出现

将以下内容翻译为中文:最小化删除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 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 

输出

The total number of minimum substrings of consecutive zeros required to remove is - 2.

登录后复制

空间复杂度 - 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 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 

输出

The total number of minimum substrings of consecutive zeros required to remove is - 2

登录后复制

结论

我们已经看到了两种不同的解决方案来解决给定的问题。在第一种方法中,我们计算连续的零对的总数,在第二种方法中,我们计算不匹配的相邻字符的总数。

以上就是将以下内容翻译为中文:最小化删除0子串以从循环二进制字符串中删除所有0的出现的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2588042.html

(0)
上一篇 2025年3月6日 15:46:29
下一篇 2025年3月6日 15:46:39

AD推荐 黄金广告位招租... 更多推荐

发表回复

登录后才能评论