码农知识堂 - 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

  • 相关阅读:
    高级架构师_Docker_第2章_ Docker核心原理_ 第6节Docker部署微服项目
    怎样理解Redis中的AOF重写?
    使用 JavaScript 切换全屏模式
    医保医用耗材编码目录——在线查询
    Docker学习笔记-概念和常见命令
    【python】任务调度编排工具 schedule | python定时任务工具
    Android JNI 异常定位(2)—— addr2line
    [图像处理] IOU Intersection over Union
    vite创建的vue3项目使用jsx
    ​python联欢会评分 青少年编程电子学会python编程等级考试三级真题解析2020年9月
  • 原文地址:https://blog.csdn.net/Tisfy/article/details/134296484
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号