696. Count Binary Substrings -- Python

本文介绍了一种算法,用于计算给定字符串中具有相同数量连续0和1的非空子串的数量,并通过示例解释了如何实现该算法。

696. 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.

Note:

  • s.length will be between 1 and 50,000.
  • s will only consist of “0” or “1” characters.

思路:
可以先将连续的 ”0“ 和 ”1“ 分离,然后再依次找个两个中的最小值就可

‘00001111’ => [4, 4] => min(4, 4) => 4
‘00110’ => [2, 2, 1] => min(2, 2) + min(2, 1) => 3
‘10101’ => [1, 1, 1, 1, 1] => 4

我的代码:(效率太低,超时,未被接受):

class Solution:
    def countBinarySubstrings(self, nums):
        """
        :type s: str
        :rtype: int
        """
        number=0
        nums_of_0 = nums.count("0")
        nums_of_1 = nums.count("1")
        for i in range(1,min(nums_of_0,nums_of_1)+1):
            x=["0"*i+"1"*i,"1"*i+'0'*i]
            count1=nums.count(x[0])
            count2=nums.count(x[1])
            number+=count1+count2
        return number    

修改版:

class Solution:
    def countBinarySubstrings(self, s):
        """
        :type s: str
        :rtype: int
        """
        l=[]
        for i in s.replace("01","0 1").replace("10","1 0").split():
            l.append(len(i))
        return sum(min(a,b) for a,b in zip(l,l[1:]))
<think>我们注意到用户给出的命令中指定平台为 `Ubuntu22.04.3 LTS`,但pip的`--platform`参数期望的是平台标签(如`manylinux`系列),而不是操作系统版本字符串。因此,我们需要更正为合适的平台标签。正确的做法是使用与Ubuntu22.04兼容的平台标签,例如`manylinux_2_31_x86_64`或`manylinux_2_24_x86_64`(因为Ubuntu22.04的glibc版本是2.35,而manylinux_2_24要求glibc>=2.24,所以是兼容的)。另外,也可以使用`linux_x86_64`,但这可能不包含对旧版glibc的兼容性保证。此外,`--python-version`应该指定为不带补丁版本的主版本号(如3.12),但pip实际上允许带补丁版本,不过通常只指定主次版本(如3.12)。但是,如果指定3.12.3,pip可能会忽略补丁号。因此,修正后的命令如下: ```cmdpipdownload -r requirements.txt ^--platformmanylinux_2_24_x86_64^--only-binary=:all: ^-doffline_packages ^--python-version3.12^--implementation cp```或者,为了兼容更多环境,可以指定多个平台:```cmdpipdownload -r requirements.txt ^--platformmanylinux_2_24_x86_64 ^--platformmanylinux2014_x86_64^--platform linux_x86_64^--only-binary=:all: ^-doffline_packages ^--python-version3.12^--implementation cp```解释: -`--platform`:指定了多个平台标签,以增加兼容性。`manylinux_2_24`和`manylinux2014`是常见的标准,`linux_x86_64`作为基础平台。 -`--only-binary=:all:`:确保只下载二进制包(wheel),不下载源码包。- `-doffline_packages`:下载到`offline_packages`目录。 -`--python-version3.12`:指定目标Python版本为3.12。注意,这里使用主次版本(3.12)即可,因为pip在匹配时不会考虑补丁版本。 -`--implementation cp`:指定为CPython(标准的Python实现)。注意:在Windows上使用`^`作为换行符,在Linux/macOS中使用`\`。但是,如果项目中包含纯Python包(没有C扩展),那么它们通常会是`any`平台的包,所以即使指定了平台,这些包也会被下载(因为它们不依赖平台)。另外,如果某个包没有提供指定平台的二进制包,pip会报错。此时,我们可以考虑同时下载源码包作为备用(使用另一个命令下载源码包到不同目录,或者使用`--no-binary`选项针对特定包下载源码,但这样可能会使命令复杂化)。或者,我们可以尝试不指定平台,让pip下载当前平台的二进制包(但这样在Ubuntu上可能无法使用,因为Windows的二进制包在Linux上不可用)。因此,在Windows上必须指定目标平台(Linux)才能下载正确的二进制包。所以,用户原来的命令中平台参数写为`Ubuntu22.04.3 LTS`是不正确的,需要替换为正确的平台标签。总结:修正后的命令使用合适的平台标签(如`manylinux_2_24_x86_64`)和Python版本(3.12)即可。下面给出一个完整的示例,包括如何检查下载的包是否包含所需平台的wheel文件:步骤1:生成requirements.txt步骤2:使用上述命令下载步骤3:检查下载的包注意:如果目标Ubuntu系统是ARM架构(如使用AppleSilicon或AWS Graviton),则需要指定`aarch64`架构的平台标签,例如`manylinux_2_24_aarch64`。但用户的问题中指定的是Ubuntu22.04,没有特别说明架构,通常默认为x86_64。因此,我们假设目标架构为x86_64(即amd64)。如果用户需要为ARM64架构准备,则需将平台标签中的`x86_64`替换为`aarch64`。最后,我们还可以编写一个脚本来检查下载的包是否包含所需的平台:```bash#!/bin/bash#检查下载目录中所有wheel文件的平台标签forwhl inoffline_packages/*.whl;do#使用wheel工具检查if! wheelshow "$whl" |grep -q "manylinux_2_24_x86_64";thenecho"WARNING: $whlmay notbe compatiblewith manylinux_2_24_x86_64"fidone```注意:需要先安装`wheel`包:`pipinstall wheel`但是,由于wheel可能支持多个平台,所以上述检查只是简单检查,更准确的做法是使用`wheelshow`来查看平台标签,然后判断是否包含我们需要的平台。由于这个检查脚本可能比较复杂,且不是必须的,所以这里只作为一个可选步骤。因此,用户应该使用修正后的命令来下载包。</think>### 分析并修正 pip download 命令 你提供的命令存在几个问题,特别是 `--platform Ubuntu 22.04.3 LTS` 这部分。pip 的 `--platform` 参数需要特定的平台标识符,而不是操作系统描述字符串。以下是修正后的方案: #### 问题分析 1. **平台标识符错误**: - `Ubuntu 22.04.3 LTS` 不是有效的 pip 平台标识符 - 正确格式应为 `manylinux` 系列标签(如 `manylinux_2_31_x86_64`) 2. **Python 版本格式**: - `--python-version` 应使用主次版本号(如 `3.12`),而不是完整版本号 3. **平台兼容性**: - Ubuntu 22.04 对应的标准平台标签是 `manylinux_2_31_x86_64` #### 修正后的命令 ```cmd pip download -r requirements.txt ^ --platform manylinux_2_31_x86_64 ^ # Ubuntu 22.04 标准平台标签 --only-binary=:all: ^ -d offline_packages ^ --python-version 3.12 ^ # 只需主次版本号 --implementation cp ``` #### 完整的最佳实践命令 ```cmd pip download -r requirements.txt ^ --platform manylinux_2_31_x86_64 ^ # Ubuntu 22.04 主要平台 --platform manylinux2014_x86_64 ^ # 兼容旧版系统 --platform linux_x86_64 ^ # 基础 Linux 平台 --only-binary=:all: ^ -d offline_packages ^ --python-version 3.12 ^ --abi cp312 ^ # 指定 ABI 兼容性 --implementation cp ``` #### 参数详细说明 | 参数 | 正确值 | 错误值 | 说明 | |------|--------|--------|------| | `--platform` | `manylinux_2_31_x86_64` | `Ubuntu 22.04.3 LTS` | Ubuntu 22.04 的标准平台标签 | | `--python-version` | `3.12` | `3.12.3` | 只需主次版本号,补丁版本会被忽略 | | `--implementation` | `cp` | - | 指定 CPython 实现(正确) | | `--abi` | `cp312` | - | 可选,指定 Python 3.12 ABI | #### 验证平台标签的方法 ```python # 检查系统支持的平台标签 import pip._internal.utils.compatibility_tags as tags # 获取当前系统支持的所有标签 system_tags = tags.get_supported() # 打印前10个标签 print("Supported platform tags:") for tag in list(system_tags)[:10]: print(tag.platform) # 输出类似 "manylinux_2_31_x86_64" ``` 在 Ubuntu 22.04 上运行此代码会显示系统支持的有效平台标签。 #### 处理多平台情况 对于需要支持多种架构的场景: ```cmd :: 同时下载 x86_64 和 ARM64 架构的包 pip download -r requirements.txt ^ --platform manylinux_2_31_x86_64 ^ --platform manylinux_2_31_aarch64 ^ --only-binary=:all: ^ -d offline_packages ^ --python-version 3.12 ``` #### 关键注意事项 1. **平台标签规范**: - Ubuntu 22.04 → `manylinux_2_31_x86_64` - Ubuntu 20.04 → `manylinux_2_24_x86_64` - 通用 Linux → `linux_x86_64` 2. **Python 版本规范**: - 使用主次版本号 `3.12`,而非 `3.12.3` - 补丁版本在包兼容性中通常不重要 3. **ABI 兼容性**: - 添加 `--abi cp312` 可确保与 Python 3.12 ABI 兼容 - 使用 `pip debug --verbose` 查看支持的 ABI 标签 4. **备用方案**: ```cmd # 如果无法确定平台标签,下载源码包 pip download -r requirements.txt ^ --no-binary :all: ^ -d offline_packages ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值