leetcode 67 Add Binary

简介: Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions                       Given two binary strings, return their sum (also a binary string).

Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions

                     

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

For example,
a = "11"
b = "1"
Return "100".


我的解决方案:

class Solution {
public:
    string addBinary(string a, string b) 
    {
        
        string result = "";
        int c = 0;
        int i = a.size() - 1;
        int j = b.size() - 1;
        
        while(i >= 0 || j >=0 ||c ==1)
        {
            c += i >= 0 ? a[i--] - '0':0;
            c += j >= 0 ? b[j--] - '0':0;
            result = char( c% 2 + '0') + result;
            c /= 2;
            
        }
        
        return result;
        
        
    }
};


c语言解决方案:

char* addBinary(char* a, char* b) {
    int n, m;
    for (n=0; *a; a++, n++) ;
    for (m=0; *b; b++, m++) ;
    char *p = (char*)malloc(m>n ? m+2 : n+2), *last = p;
    int c = 0;
    while (n || m || c) {
        int s = c;
        if (n) {
            s += *(--a)-'0';
            --n;
        }
        if (m) {
            s += *(--b)-'0';
            --m;
        }
        *last++ = (s&1)+'0';
        c = s>>1;
    }
    *last=0;
    char *start = p, t;
    while (start+1 < last) { // reverse string
        t = *start;
        *start++=*(--last);
        *last=t;
    }
    return p;
}

数字电路版本代码:加法器的实现
https://leetcode.com/discuss/40846/c-4ms-solution-inspired-by-hardware-full-adder-circuit



python 的三个版本:


class Solution:
    # @param {string} a
    # @param {string} b
    # @return {string}
    def addBinary(self, a, b):
        i, m, n, result, carry = 1, len(a), len(b), [], 0
        while i <= m or i <= n:
            temp = carry
            if i <= m:
                temp += int(a[-i])
            if i <= n:
                temp += int(b[-i])

            carry = temp / 2
            result.append(str(temp % 2))
            i += 1

        if carry:
            result.append(str(carry))

        return ''.join(result[::-1])

or a really short one if you want

class Solution:
    # @param {string} a
    # @param {string} b
    # @return {string}
    def addBinary(self, a, b):
        return '{0:b}'.format(int(a, 2) + int(b, 2))


class Solution:
    # @param {string} a
    # @param {string} b
    # @return {string}
    def addBinary(self, a, b):
        return bin(int(a,2) + int(b,2))[2:]





相关文章
|
5月前
Leetcode Minimum Depth of Binary Tree (面试题推荐)
计算树的最小深度 很简单的一道题,只需要遍历一次树,到叶子节点的时候计算一下深度和当前最小深度比较,保存最小值就行。 我在这用了一个全局变量 mindepth。总感觉我这代码写的不够简练,求更精简的方法。
22 0
|
5月前
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
15 0
|
5月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
24 0
|
7月前
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
10月前
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 102. 二叉树的层序遍历 Binary Tree Level Order Traversal
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree