0

0

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

WBOY

WBOY

发布时间:2023-08-25 15:41:18

|

705人浏览过

|

来源于tutorialspoint

转载

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

    TextIn Tools
    TextIn Tools

    是一款免费在线OCR工具,包含文字识别、表格识别,PDF转文件,文件转PDF、其他格式转换,识别率高,体验好,免费。

    下载
  • 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 < 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.

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

结论

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

相关专题

更多
string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

312

2023.08.02

typedef和define区别
typedef和define区别

typedef和define区别在类型检查、作用范围、可读性、错误处理和内存占用等。本专题为大家提供typedef和define相关的文章、下载、课程内容,供大家免费下载体验。

100

2023.09.26

define的用法
define的用法

define用法:1、定义常量;2、定义函数宏:3、定义条件编译;4、定义多行宏。更多关于define的用法的内容,大家可以阅读本专题下的文章。

312

2023.10.11

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

711

2023.08.22

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

81

2023.09.25

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

248

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

205

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1434

2023.10.24

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
R 教程
R 教程

共45课时 | 4.2万人学习

Webpack4.x---十天技能课堂
Webpack4.x---十天技能课堂

共20课时 | 1.4万人学习

进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号