码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode 2609. 最长平衡子字符串


    【LetMeFly】2609.最长平衡子字符串

    力扣题目链接:https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string/

    给你一个仅由 0 和 1 组成的二进制字符串 s 。  

    如果子字符串中 所有的 0 都在 1 之前 且其中 0 的数量等于 1 的数量,则认为 s 的这个子字符串是平衡子字符串。请注意,空子字符串也视作平衡子字符串。 

    返回  s 中最长的平衡子字符串长度。

    子字符串是字符串中的一个连续字符序列。

     

    示例 1:

    输入:s = "01000111"
    输出:6
    解释:最长的平衡子字符串是 "000111" ,长度为 6 。
    

    示例 2:

    输入:s = "00111"
    输出:4
    解释:最长的平衡子字符串是 "0011" ,长度为  4 。
    

    示例 3:

    输入:s = "111"
    输出:0
    解释:除了空子字符串之外不存在其他平衡子字符串,所以答案为 0 。
    

     

    提示:

    • 1 <= s.length <= 50
    • '0' <= s[i] <= '1'

    方法一:字符串处理

    “平衡字符串”的前提是数个0后面有数个1。因此,我们可以使用一个变量index来存储当前处理到的字符,每次遍历完所有相连的0后遍历所有相邻的1,其中0和1的最小值的二倍即为当前“平衡子字符串”的长度。

    index = 0
    while index < len(s):
        cnt0 = 0
        while index < len(s) and s[index] == '0':  # 遍历完所有的0
            cnt0++, index++
        while index < len(s) and s[index] == '1':  # 遍历完所有的0
            cnt1++, index++
    	thisLength = 2 * min(cnt0, cnt1)
    	# 更新answer
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 时间复杂度 O ( l e n ( s ) ) O(len(s)) O(len(s))
    • 空间复杂度 O ( 1 ) O(1) O(1)

    AC代码

    C++
    class Solution {
    public:
        int findTheLongestBalancedSubstring(string s) {
            int ans = 0, index = 0;
            while (index < s.size()) {
                int cnt0 = 0, cnt1 = 0;
                while (index < s.size() && s[index] == '0') {
                    cnt0++, index++;
                }
                while (index < s.size() && s[index] == '1') {
                    cnt1++, index++;
                }
                ans = max(ans, 2 * min(cnt0, cnt1));
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    Python
    class Solution:
        def findTheLongestBalancedSubstring(self, s: str) -> int:
            ans, index = 0, 0
            while index < len(s):
                cnt0, cnt1 = 0, 0
                while index < len(s) and s[index] == '0':
                    cnt0, index = cnt0 + 1, index + 1
                while index < len(s) and s[index] == '1':
                    cnt1, index = cnt1 + 1, index + 1
                ans = max(ans, 2 * min(cnt0, cnt1))
            return ans
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
    Tisfy:https://letmefly.blog.csdn.net/article/details/134296484

  • 相关阅读:
    Jedis操作Redis
    第14章 软件测试过程和管理
    浅谈地下污水处理厂电气特点和能效管理系统的实际应用
    IDM下载器怎么用 IDM下载器使用技巧 idm下载器怎么下载网页视频
    Python学习3(函数、装饰器)
    JavaScript算法44- 最小处理时间(leetCode:2895middle)周赛
    Linux中的进程终止(详解)
    c语言进阶:指针的进阶(下)
    可视化3个10分类
    Jenkins从配置到实践(2022尚硅谷Jenkins学习笔记)
  • 原文地址:https://blog.csdn.net/Tisfy/article/details/134296484
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号