首页 > 其他分享 >leetcode-696-easy

leetcode-696-easy

时间:2022-11-26 20:34:30浏览次数:40  
标签:count arr 696 int number length easy leetcode first

Count Binary Substrings

Given a binary string s, return the number of non-empty substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.

Substrings that occur multiple times are counted the number of times they occur.

Example 1:

Input: s = "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
Notice that some of these substrings repeat and are counted the number of times they occur.
Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
Example 2:

Input: s = "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
Constraints:

1 <= s.length <= 105
s[i] is either '0' or '1'.

思路一:先预处理字符串,统计 0 和 1 出现的次数,结果放入到一个数组中,有个统计的数组可以求结果,如下

对于字符串 "00110011"
生成数组 [0, 2, 0, 2, 0, 2, 0, 2]
观察数组可知,对于不为 0 的前后两个统计数字,总数=Math.min(first, second)
public int countBinarySubstrings(String s) {
    char[] chars = s.toCharArray();
    int[] arr = new int[chars.length];

    int count = 1;
    for (int i = 1; i < chars.length; i++) {
        if (chars[i] == chars[i - 1]) {
            count++;
        } else {
            arr[i - 1] = count;
            count = 1;
        }
    }

    if (arr[arr.length - 1] == 0) {
        arr[arr.length - 1] = count;
    }

    int first = -1;
    count = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == 0) continue;

        if (first == -1) {
            first = i;
        } else {
            count += Math.min(arr[first], arr[i]);
            first = i;
        }
    }

    return count;
}

标签:count,arr,696,int,number,length,easy,leetcode,first
From: https://www.cnblogs.com/iyiluo/p/16928247.html

相关文章

  • leetcode-796-easy
    RotateStringGiventwostringssandgoal,returntrueifandonlyifscanbecomegoalaftersomenumberofshiftsons.Ashiftonsconsistsofmovingthe......
  • leetcode-1299-easy
    ReplaceElementswithGreatestElementonRightSideGivenanarrayarr,replaceeveryelementinthatarraywiththegreatestelementamongtheelementstoit......
  • leetcode-110-easy
    BalancedBinaryTreeGivenabinarytree,determineifitisheight-balanced.Example1:Input:root=[3,9,20,null,null,15,7]Output:trueExample2:Input......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:反转字符串中的单词 III
    题目:给定一个字符串 s ,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例1:输入:s="Let'stakeLeetCodecontest"输出:"s'teLekatedoCteeL......
  • easylogging++的那些事(四)源码分析(二)日志记录宏(一)CLOG宏(四)日志信息保存
    目录writer类的输出运算符writer类的流操控符el::base::MessageBuilder类CLOG宏接口调用流程图在上一篇中我们分析完了CLOG宏日志输出的流程,在结尾的时候我们提......
  • leetcode_D3_27移除元素
    1.题目   2.解一   主要思路:解一为本人解法,主要思路是先利用循环删除掉所有数组中值等于val的元素,然后可以直接返回数组的长度和其中的元素。感觉是没经过......
  • 二分查找-LeetCode704 简单题
    LeetCode代码链接:https://leetcode.cn/problems/binary-search/题目:给定一个 n 个元素有序的(升序)整型数组 nums和一个目标值 target ,写一个函数搜索 nums 中的t......
  • [LeetCode] 809. Expressive Words
    Sometimespeoplerepeatletterstorepresentextrafeeling.Forexample:"hello"->"heeellooo""hi"->"hiiii"Inthesestringslike "heeellooo",wehaveg......
  • leetcode 24. 两两交换链表中的节点 js实现
    给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。示例1:输入:head=[1,2,3,4]输出:[2,......
  • BUU 刷题之 护网杯 2018]easy_tornado 1
    今天打靶场第一次碰到这个题,就想记录下。虽然做题的过程基本都是照着WP做的。。。。。。首先我们进入这个题目,上面有三个文件链接,指出了flag的位置以及加密方式。还有rend......