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. scroll的应用

    jQuery(document).ready(function($){ $('#shang').click(function(){ $('html,body').animate({scrollTop: ...

  2. Farseer.net轻量级开源框架 入门篇:删除数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 ...

  3. DVWA--登录页面错误问题 469 | | PHP Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\web\DVWA\dvwa\includes\dvwaPage.inc.php:469

    // MySQL PDO Prepared Statements (for impossible levels) $db = new PDO('mysql:host=' . $_DVWA[ 'db_s ...

  4. Intel Processor Exception Handling

    当一个进程的行为超出预期时,系统将把它kill掉. On Intel IA-32 and Intel 64 architecture processors, each architecturally- ...

  5. 前端零基础快速入门JavaScript

    JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中: <html><head> <script&g ...

  6. 扩增子分析解读4去嵌合体 非细菌序列 生成代表性序列和OTU表

    本节课程,需要先完成 扩增子分析解读1质控 实验设计 双端序列合并 2提取barcode 质控及样品拆分 切除扩增引物 3格式转换 去冗余 聚类   先看一下扩增子分析的整体流程,从下向上逐层分析 分 ...

  7. jquery 五星评价(图片实现)

    1111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  8. ArrayList经典Demo

    import java.util.ArrayList; import java.util.Iterator; public class ArrayListDemo { public static vo ...

  9. Linux设置history命令显示行数以及时间

    Linux和unix上都提供了history命令,可以查询以前执行的命令历史记录但是,这个记录并不包含时间项目因此只能看到命令,但是不知道什么时间执行的如何让history记录时间呢? 解决方案 注意 ...

  10. (七)python3 切片

    切片:取一个 list 或 tuple 的部分元素是非常常见的操作 >>> L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] #笨办法 ...