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 addBinary(string a, string b) {
        reverse(begin(a), end(a));
        reverse(begin(b), end(b));
        string result;
        char num = ‘0‘;
        int i = 0;
        for (;i < a.size() && i < b.size();++i)
        {
            if (a[i] == ‘0‘&&b[i] == ‘0‘&&num == ‘0‘)
            {
                result.insert(begin(result), ‘0‘);
            }
            if (a[i] == ‘1‘&&b[i] == ‘1‘&&num == ‘1‘)
            {
                result.insert(begin(result), ‘1‘);
            }
            if ((a[i] == ‘0‘&&b[i] == ‘1‘&&num == ‘1‘) || (a[i] == ‘1‘&&b[i] == ‘0‘&&num == ‘1‘) ||
                (a[i] == ‘1‘&&b[i] == ‘1‘&&num == ‘0‘))
            {
                result.insert(begin(result), ‘0‘);
                num = ‘1‘;
            }
            if (a[i] == ‘1‘&&b[i] == ‘0‘&&num == ‘0‘ || a[i] == ‘0‘&&b[i] == ‘1‘&&num == ‘0‘ ||
                a[i] == ‘0‘&&b[i] == ‘0‘&&num == ‘1‘)
            {
                result.insert(begin(result), ‘1‘);
                num = ‘0‘;
            }
        }
        if (i == a.size())
        {
            for (;i < b.size();++i)
            {
                if (b[i] == ‘0‘&&num == ‘0‘)
                    result.insert(begin(result), ‘0‘);
                if (b[i] == ‘0‘&&num == ‘1‘ || b[i] == ‘1‘&&num == ‘0‘)
                {
                    result.insert(begin(result), ‘1‘);
                    num = ‘0‘;
                }
                if (b[i] == ‘1‘&&num == ‘1‘)
                    result.insert(begin(result), ‘0‘);
            }
        }
        else
        {
            for (;i < a.size();++i)
            {
                if (a[i] == ‘0‘&&num == ‘0‘)
                    result.insert(begin(result), ‘0‘);
                if (a[i] == ‘1‘&&num == ‘1‘)
                    result.insert(begin(result), ‘0‘);
                if (a[i] == ‘0‘&&num == ‘1‘ || a[i] == ‘1‘&&num == ‘0‘)
                {
                    result.insert(begin(result), ‘1‘);
                    num = ‘0‘;
                }

            }
        }
        if (num == ‘1‘)result.insert(begin(result), ‘1‘);
        return result;
    }
};
时间: 2024-12-28 05:36:50

LeetCode 67. Add Binary的相关文章

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

LeetCode 67. Add Binary (二进制相加)

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题目标签:Math 题目给了我们两个string a 和 b,让我们把这两个二进制 相加. 首先把两个string 的长度得到,然后从右向左 取 两个string 的 digit. 增设一个 carry = 0: 每一轮把 digit

LeetCode 67 Add Binary(二进制相加)(*)

翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 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". 思路:二进制加法,比较简单.代码如下: public class Solution { public String addBinary(String a, String b) { int len = Math.max(a.len

[leetcode] 67. Add Binary 解题报告

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 判断数字时用charAt(i)-'0'即可 public String addBinary(String a, String b) { StringBuilder sb = new StringBuilder(); int i=a.l

leetcode 67. 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 ans=""; int c=0,i=a.len

[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). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&q

19.2.12 [LeetCode 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. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&q