码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题——2609. Find the Longest Balanced Substring of a Binary String


    文章目录

      • 一、题目
      • 二、题解

    一、题目

    You are given a binary string s consisting only of zeroes and ones.

    A substring of s is considered balanced if all zeroes are before ones and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.

    Return the length of the longest balanced substring of s.

    A substring is a contiguous sequence of characters within a string.

    Example 1:

    Input: s = “01000111”
    Output: 6
    Explanation: The longest balanced substring is “000111”, which has length 6.
    Example 2:

    Input: s = “00111”
    Output: 4
    Explanation: The longest balanced substring is “0011”, which has length 4.
    Example 3:

    Input: s = “111”
    Output: 0
    Explanation: There is no balanced substring except the empty substring, so the answer is 0.

    Constraints:

    1 <= s.length <= 50
    ‘0’ <= s[i] <= ‘1’

    二、题解

    class Solution {
    public:
        int findTheLongestBalancedSubstring(string s) {
            int n = s.length();
            int res = 0;
            for(int i = 0;i < n;i++){
                //过滤到字符串最前面的1
                if(s[i] - '0' == 1) continue;
                int zeroCount = 0;
                int oneCount = 0;
                //统计0的数量
                while(s[i] - '0' == 0 && i < n) zeroCount++,i++;
                //统计1的数量
                while(s[i] - '0' == 1 && i < n) oneCount++,i++;
                i--;
                res = max(res,min(zeroCount,oneCount) * 2);
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    一文带您快速了解工业交换机
    驱动有没有(静态和动态加载)
    大数据-各类图像数据集下载地址
    为什么要code review
    ipv6过渡技术-IPv4 over IPv6隧道示例
    如何快速开发一个简单实用的MES系统?
    jsencrypt 公私钥解加密
    教你把mov格式的视频转换mp4
    深度学习 一:Deep Learning基本概念及线性、非线性回归对比分析(sigmoid v.s. ReLU)
    数字人ai直播软件突破AI大模型技术,改变未来科技格局!
  • 原文地址:https://blog.csdn.net/weixin_46841376/article/details/134293846
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号