No67. Add Binary(Python)

本文介绍了一种解决两个二进制字符串相加的问题的方法。通过遍历输入的二进制字符串并应用二进制加法规则,实现了一个Python类方法来计算两个二进制字符串的和,并返回结果作为新的二进制字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

思路:

        二进制加法思想就是:从最低位两位数相加,到2后进一位加到最高项。这就是代码的整体思路。

代码:

class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        aIndex,bIndex,flag,s = len(a)-1,len(b)-1,0,''
        while aIndex >= 0 and bIndex >= 0:
            num = int(a[aIndex])+int(b[bIndex])+flag
            flag = num/2
            num %= 2
            s = str(num)+s
            aIndex -= 1
            bIndex -= 1
        while aIndex >= 0:
            num = int(a[aIndex])+flag
            flag = num/2
            num %= 2
            s = str(num)+s
            aIndex -= 1
        while bIndex >= 0:
            num = int(b[bIndex])+flag
            flag = num/2
            num %= 2
            s = str(num)+s
            bIndex -= 1
        if flag == 1:
            s = '1'+ s
        return s


Traceback (most recent call last): File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/selenium/webdriver/common/driver_finder.py", line 67, in _binary_paths output = SeleniumManager().binary_paths(self._to_args()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/selenium/webdriver/common/selenium_manager.py", line 46, in binary_paths args = [str(self._get_binary())] + args ^^^^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/selenium/webdriver/common/selenium_manager.py", line 93, in _get_binary raise WebDriverException(f"Unsupported platform/architecture combination: {sys.platform}/{arch}") selenium.common.exceptions.WebDriverException: Message: Unsupported platform/architecture combination: linux/aarch64 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/data/user/0/com.cscjapp.python/files/ide_files/ide_run.py", line 31, in <module> start(fakepyfile,mainpyfile) File "/data/user/0/com.cscjapp.python/files/ide_files/ide_run.py", line 30, in start exec(open(mainpyfile).read(), __main__.__dict__) File "<string>", line 5, in <module> File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 47, in __init__ super().__init__( File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 53, in __init__ if finder.get_browser_path(): ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/selenium/webdriver/common/driver_finder.py", line 47, in get_browser_path return self._binary_paths()["browser_path"] ^^^^^^^^^^^^^^^^^^^^ File "/data/user/0/com.cscjapp.python/files/aarch64-linux-android/lib/python3.11/site-packages/selenium/webdriver/common/driver_finder.py", line 78, in _binary_paths raise NoSuchDriverException(msg) from err selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location /storage/emulated/0/Android/data/com.cscjapp.python/files/CJ_IDE/PythonProject/默认目录/王爷,计划/src $
最新发布
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值