码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 67 Add Binary

时间:2017-04-11 13:28:39      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:sub   src   wan   eal   problem   res   code   example   targe   

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:]




??

leetcode 67 Add Binary

标签:sub   src   wan   eal   problem   res   code   example   targe   

原文地址:http://www.cnblogs.com/gccbuaa/p/6692872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!