67. Add Binary Leetcode Python

For example,

a = "11"

b = "1"

Return "100".

这题的做法和前面一题plus one 一样,区别在于需要将两个string先相加,再判断是否要进位。

直接上代码吧。

class Solution:
    # @param a, a string
    # @param b, a string
    # @return a string
    def addBinary(self, a, b):
        a=list(a)
        b=list(b)
        for i in range(len(a)):
            a[i]=int(a[i])
        for i in range(len(b)):
            b[i]=int(b[i])
        lena=len(a)
        lenb=len(b)
        if lena<lenb:
            a,b=b,a
        itera=len(a)-1
        iterb=len(b)-1
        while itera>=0 and iterb>=0:
            a[itera]+=b[iterb]
            itera-=1
            iterb-=1
        for i in reversed(range(len(a))):
            digit=a[i]%2
            carry=a[i]/2
            a[i]=digit
            if i>0:
                a[i-1]+=carry
        if carry==1:
            a.insert(0,1)
        for i in range(len(a)):
            a[i]=str(a[i])
        return "".join(a)
时间: 2024-10-10 10:14:06

67. Add Binary Leetcode Python的相关文章

67. Add Binary [easy] (Python)

题目链接 https://leetcode.com/problems/add-binary/ 题目原文 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题目翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如:a = "11",b = &q

67. Add Binary【LeetCode】

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 1 public class Solution { 2 public String addBinary(String a, String b) { 3 String res ="";

leetCode 67. Add Binary 字符串

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 1.将两个字符串按数组相加得到新数组. 2.将新数组转换成结果. 代码如下: class Solution { public:     string addBinary(string a, str

Add Binary Leetcode java

题目: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题解: 二进制加法都是从最低位(从右加到左).所以对两个字符串要从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0. 每轮计算要加上进位,最后跳出循环后要坚持进位是否为1,以便更新结果. 代码如下(from

No.67 Add Binary

No.67 Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 与No.2 Add Two Numbers的思路非常相似,不同在:对其进行运算时,其实是要逆向相加的!!!因为边界控制的原因,可先将字符串逆序reverse( ). 1 #include "

LeeCode from 0 —— 67. Add Binary

67. Add Binary 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. 解题思路: 1)将每一位字符都转换为数字,将两个字符串对应位的数字相加.若需要进位,则保存进位的值并在下一次相加时加上进位.对应位若不存在则设为0,相加. 2)若两个字符串所有对应位相加结

LeetCode 67. Add Binary (二进制相加)

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题目标签:Math 题目给了我们两个string a 和 b,让我们把这两个二进制 相加. 首先把两个string 的长度得到,然后从右向左 取 两个string 的 digit. 增设一个 carry = 0: 每一轮把 digit

[LeetCode] 67. Add Binary 二进制数相加

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 从最低位加到最高位,当前位相加结果是%2,进位是/2,记得处理每一次的进位和最后一次的进位,最后反向输出字符. Java: public class AddBinary { public String addBinary(St

LeetCode 67. Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 这个题目只要注意各种情况你就成功了一大半,特别要注意的是对进位赋值后可能产生的变化,以及最后一位进位为1时, 我们要把这个1插进来.同时注意字符串的低位是我们数值的高位 class Solution { public: string