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 = “1”,返回 “100”。

思路方法

思路一

利用Python的进制转换函数,先将两个加数转成10进制,再把和转换成二进制返回即可。虽然速度还挺快的,但这么做忽略了可能的大整数相加的细节(因为Python帮你处理了)。

代码

class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        return bin(int(a, 2) + int(b, 2))[2:]

思路二

从两个字符串的最低位开始,一位一位的进行二进制相加,并保存进位,最终可以得到两者的和的字符串。

代码

class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        res = ‘‘
        i, j, plus = len(a)-1, len(b)-1, 0
        while i>=0 or j>=0 or plus==1:
            plus += int(a[i]) if i>= 0 else 0
            plus += int(b[j]) if j>= 0 else 0
            res = str(plus % 2) + res
            i, j, plus = i-1, j-1, plus/2
        return res

思路三

用递归实现,要注意,当两个加数都是1时要进位。

代码

class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        if not a or not b:
            return a if a else b
        if a[-1] == ‘1‘ and b[-1] == ‘1‘:
            return self.addBinary(self.addBinary(a[:-1], b[:-1]), ‘1‘) + ‘0‘
        elif a[-1] == ‘0‘ and b[-1] == ‘0‘:
            return self.addBinary(a[:-1], b[:-1]) + ‘0‘
        else:
            return self.addBinary(a[:-1], b[:-1]) + ‘1‘

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!

转载请注明:http://blog.csdn.net/coder_orz/article/details/51706532

时间: 2024-08-08 13:54:28

67. Add Binary [easy] (Python)的相关文章

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

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

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)若两个字符串所有对应位相加结

67. Add Binary

1. 问题描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Tags: Math String 2. 解答思路 3. 代码 1 #include <stack> 2 #include <string> 3 #include <cctype&g

[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

【LeetCode】67 - Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". Solution:二进制加法,和为2进1,和为3进1留1: 1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 int sizea=a.siz