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 a + digit b + carry = sum:

    新的 digit = sum % 2;

    新的 carry = sum / 2;

  注意当两个string 都走完时候,还要检查一下carry, 是否需要加上digit。

Java Solution:

Runtime beats 42.60%

完成日期:12/11/2017

关键词:Math

关键点:digit = sum % 2; carry = sum / 2

 class Solution
{
public String addBinary(String a, String b)
{
StringBuilder sb = new StringBuilder();
int aLen = a.length() - 1;
int bLen = b.length() - 1;
int carry = 0; while(aLen >= 0 || bLen >= 0)
{
int sum = carry; if(bLen >= 0)
sum += b.charAt(bLen--) - '0';
if(aLen >= 0)
sum += a.charAt(aLen--) - '0'; sb.insert(0, sum % 2);
carry = sum / 2;
} // for last carry
if(carry != 0)
sb.insert(0, carry); return sb.toString();
}
}

参考资料:https://discuss.leetcode.com/topic/13698/short-ac-solution-in-java-with-explanation

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 67. Add Binary (二进制相加)的更多相关文章

  1. [LeetCode] 67. Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  2. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  3. [leetcode]67. Add Binary 二进制加法

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

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

    翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...

  5. (String) leetcode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  6. leetcode 67. Add Binary (高精度加法)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  7. Leetcode 67 Add Binary 大数加法+字符串处理

    题意:两个二进制数相加,大数加法的变形 大数加法流程: 1.倒置两个大数,这一步能使所有大数对齐 2.逐位相加,同时进位 3.倒置两个大数的和作为输出 class Solution { public: ...

  8. LeetCode 67. Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  9. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

随机推荐

  1. java操作zip文件

    思路: 1).读取zip中的文件并将除了重名文件之外的文件转存到中转zip文件中. 2).往中转文件中插入txt文件. 3).删除原zip文件. 4).将中转zip文件重命名为原zip文件. 前提,t ...

  2. js类型识别

    typeof总结: 可以识别标准类型(Null除外) 不能识别具体的对象类型(Function除外) Object.prototype.toString总结: 可以识别标准类型和内置对象类型 不能识别 ...

  3. 用PHP开发自己的独立博客(一)——概述

    开篇废话:因为重新回归朝九晚五的生活,于是就想开始写技术博客,当是做技术文档了.于是试用了各类博客,CSDN.cnblogs都还不错.简单试用了一下,说说各自的特点. CSDN的界面不能定制,使用默认 ...

  4. swift- mutating

    struct Stack<Element> { var items = [Element]() func push(_ item:Element){ self.items.append(i ...

  5. discuz x3论坛搬家换虚拟主机完美使用教程 亲测可行 附操作步骤

    第一步:备份网站数据进入后台—站长—数据库—备份,数据备份类型选择“Discuz!和 UCenter数据”,备份成功以后,数据自动保存在data文件夹下. 第二步:网站文件下载 把整个网站文件打包(虚 ...

  6. 并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理

    1. 概述 1.1 什么是线程池 与jdbc连接池类似,在创建线程池或销毁线程时,会消耗大量的系统资源,因此在java中提出了线程池的概念,预先创建好固定数量的线程,当有任务需要线程去执行时,不用再去 ...

  7. 打造个人的vimIDE

    环境说明 系统版本:centos7.Ubuntu16 vim版本:7.4 安装git工具 整体说明:本文的vim配置是针对Linux的单个系统用户,python的自动补全使用的是 jedi-vim 插 ...

  8. CentOS下安装微软雅黑字体

    CentOS下安装微软雅黑字体   微软雅黑下载地址:http://download.csdn.net/detail/u012547633/9796219 1.先从你本机 C:\Windows\Fon ...

  9. 【搜索、bfs】Find The Multiple

    Problem   Given a positive integer n, write a program to find out a nonzero multiple m of n whose de ...

  10. 「 poj 2096 」 Collecting Bugs

    先说一下题意 $s$ 个子系统还中有 $n$ 种 $\text{bug}$,每天可以随机选择一种 $\text{bug}$,问选出 $n$ 种 $\text{bug}$ 在 $s$ 种子系统中的期望天 ...