LeetCode(67)题解: Add Binary

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".

思路:

数组模拟加法操做,注意首位进一的情况。

class Solution {
public:
    string addBinary(string a, string b) {
        string c="",d="";
        if(a=="0"&&b=="0")
            return "0";
        int a_t=a.size(),b_t=b.size(),min_t=min(a_t,b_t),max_t=max(a_t,b_t);
        int tmp_a,tmp_b,jinyi=0;
        for(int i=0;i<max_t;i++){
            if(i<a_t)
                tmp_a=a[a_t-i-1]-‘0‘;
            else
                tmp_a=0;
            if(i<b_t)
                tmp_b=b[b_t-i-1]-‘0‘;
            else
                tmp_b=0;
            c.push_back((tmp_a+tmp_b+jinyi)%2+‘0‘);
            if(tmp_a+tmp_b+jinyi>=2)
                jinyi=1;
            else
                jinyi=0;
        }
        if(jinyi==1)
            c.push_back(‘1‘);
        for(int i=c.size()-1;i>=0;i--)
            d.push_back(c[i]);
        return d;
    }
};
时间: 2024-11-29 04:07:29

LeetCode(67)题解: Add Binary的相关文章

leetcode || 67、Add Binary

problem: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". Hide Tags Math String 题意:二进制数相加,二进制数用string表示 thinking: (1)这题简单,但是有一个坑很容易掉进去:二进制的数的低位在最右边,string的下标低位再最左边

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【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); //

LeetCode OJ:Add Binary(二进制相加)

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 简单的二进制相加而已,只不过传入的参数是字符串而已.为了方便,先将string  reverse了一下,代码如下: 1 class Solution { 2 public: 3 string addBinary(string a, s

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

[LeetCode] [Add Binary 2012-04-02 ]

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". string 的操作,短string补位.两个"0"会输出一个"00",要特殊处理,plus如果最后为"1",要补上. ? 1 2 3 4 5 6 7 8 9 10 1

【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