Python3解leetcode Count Binary Substrings
问题描述:
Give a string s, count the number of non-empty (contiguous) 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: "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: "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
思路:
由于s中只有0或1,并且要组成对必须是相邻的0和1才可以,基于这点,只考虑相邻的0、1即可
数量如何确定呢?明显有多少个1,就需要配置多少个0,故相邻01组合最终能凑成多少对,看0和1谁的个数最少即可。
代码:
class Solution:
def countBinarySubstrings(self, s: str) -> int:
s = list(map(len, s.replace('', '1 0').replace('', '0 1').split()))
return sum(min(i, j) for i, j in zip(s, s[1:]))
第一行代码,将01的字符串分组,每组仅仅包含0或仅仅包含1。同时计算每组中元素的个数
第二行代码,zip将相邻两个分组组合起来(每个分组中存储的是该分组元素个数),每个组合中较小的数字代表 该组组合能够拆成的符合条件的子串个数,然后将所有组合能够拆成的子串个数相加,得到的结果即是最终所有的个数
Python3解leetcode Count Binary Substrings的更多相关文章
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode Count Binary Substrings
原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...
- Python3解leetcode Count Primes
问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- LeetCode 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- LeetCode算法题-Count Binary Substrings(Java实现)
这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
随机推荐
- python-笔记-内置函数
###内置函数 print(all([1,2,3,4]))判断可迭代的对象里面的值是否都为真 print(any([0,1,2,3,4]))判断可迭代的对象里面的值是否有一个为真 print(id(l ...
- 锐捷网络自动连接python脚本
1 实现锐捷网络的连接,当断开后自动重连 import os import sys import time ip = 'www.baidu.com' print('开始ping百度') backinf ...
- VUE 全局监听sessionStorage变化
在做项目的时候,可能需要在其他模块获取推送的信息或者变量,但是数据量或者说数目少,而且项目中也没有引用VUEX,那么可以下手的方法之一也就是sessionStorage类的浏览器存储了. 首先在全局的 ...
- day15生成器send方法,递归,匿名函数,max结合匿名工作原理,常用的内置函数
复习 ''' 1.带参装饰器 - 自定义 | wraps def wrap(info) def outer1(func): from functools import wraps @wraps(fun ...
- quartz任务调度的详解
1.Quartz包含3个核心(调度器.作业类.触发器) (1).作业类:只需要实现org.quartz.job接口,同时包含里面的一个方法体execute()[这是被调度的作业体] (2).调度器:是 ...
- selenium安装及环境搭建
说明:安装selenium前提必须是安装好了python和pip 1.安装python 在Python的官网 www.python.org 中找到最新版本的Python安装包(我的电脑是windows ...
- 攻防世界--The_Maya_Society
测试文件:https://adworld.xctf.org.cn/media/task/attachments/17574fc423474b93a0e6e6a6e583e003.zip 我们直接将Li ...
- js中的offsetWidth用法
offsetWidth //返回盒模型的宽度(包括width+左右padding+左右border) <style> #div1 { width:200px; height:200px; ...
- express 设置允许跨域访问
//demo const express = require('express'); const app = express(); //设置允许跨域访问该服务. app.all(’’, functio ...
- motd - 当日消息
描述 (DESCRIPTION) 在 登录 系统 后, 执行 登录 shell 前, login(1) 显示 /etc/motd 中的 内容. "motd" 意思是 "m ...