LeetCode【67】Add Binary

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

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

看起来挺好写的样子,没想到墨迹了半天。

string add(string a, string b,int lena,int lenb)
{
    if(lena<lenb)
        return add(b,a,lenb,lena);
    //lena>=lenb
    int cf=0;
    int i=0,j=0;
    int ia=0,ib=0,csum=0;
    for(i=lena-1,j=lenb-1;j>=0;i--,j--)
    {
        ia=a[i]-‘0‘;
        ib=b[j]-‘0‘;
        if(cf)
            csum=ia+ib+cf;
        else
            csum=ia+ib;
        if(csum>=2)
        {
            cf=1;
            csum=csum-2;
        }
        else
            cf=0;
        a[i]=csum+‘0‘;
    }
    while(cf&&i>=0)
    {
        ia=a[i]-‘0‘;
        csum = ia+cf;
        if(csum>=2)
        {
            csum=csum-2;
            cf=1;
        }
        else
        {
            cf=0;
        }
        a[i]=csum+‘0‘;
        i--;
    }
    if(cf)
        a.insert(a.begin(),‘1‘);
    return a;
}
string addBinary(string a, string b)
{
    if(a=="" && b=="")
        return "";
    else if(a=="")
        return b;
    else if (b=="")
        return a;
    else
    {
        int lena=a.size(),lenb=b.size();
        string s=add(a,b,lena,lenb);
        return s;
    }
}

去看看有什么别人的方法:
先进行对齐补零,这样比我少了一点麻烦,没想到。。。。

看代码:

string addBinary(string a, string b) {
        int sizea = a.size();
        int sizeb = b.size();
        if(sizea < sizeb)
            return addBinary(b, a);
        //sizea >= sizeb
        string zeros(sizea-sizeb, ‘0‘);
        b = zeros + b;
        int carry = 0;
        for(int i = sizea-1; i >= 0; i --)
        {
            int sum = (a[i]-‘0‘) + (b[i] - ‘0‘) + carry;
            if(sum == 0)
                ;
            else if(sum == 1)
            {
                a[i] = ‘1‘;
                carry = 0;
            }
            else if(sum == 2)
            {
                a[i] = ‘0‘;
                carry = 1;
            }
            else
            {//sum == 3 (1+1+1)
                a[i] = ‘1‘;
                carry = 1;
            }
        }
        if(carry == 1)
            a = "1" + a;
        return a;
    }
时间: 2024-11-14 08:25:22

LeetCode【67】Add Binary的相关文章

LeetCode 第 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". 两个字符串,计算加法.这道题主要是考察对字符串操作的掌握情况.另外,加法要从低位算起,但是输出时要先输出高位.因此,需要将计算结果先存下来,然后再逆序输出. 访问字符串的

LeetCode【2】. Add Two Numbers--java实现

第二道题 Add Two Numbers 如下:         You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.     Inpu

LeetCode【2】Add two numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

【leetcode刷题笔记】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题解:简单的二进制加法模拟.a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1. 代码如下: 1 public class Solution { 2 public Str

【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal

原题: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:和上一题一样,后续我们可以通过最后一个值得到根的值,同样可以通过定位根的值得到左右子树的子集,递归求解即可. 代码: /** * Definition for binary tree * struct Tre

【Leetcode长征系列】Balanced Binary Tree

原题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1. 思路:递归判断左右子树是否为BST. 代码: /** * Def

leetcode学习笔记:Add Binary

一.题目描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 二.解题技巧 这道题考察两个二进制数相加,考虑到输入的是两组string,同时注意在运算时从左到右分别是从低位到高位,因此需要考虑对输入进行翻转处理,中间二进制树相加部分没有过多的设计障碍,主要是计算进位:在两组数

【LeetCode415】Add Strings

题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: 1 public class LeetCode415 { 2 public static void main(String[] args) { 3 String a="1",b="9"; 4 System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addStrings(a, b)); 5