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. JQuery---选择器、DOM节点操作练习

    一.练习一 1.需求效果分析: 2.代码示例: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...

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

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

  3. Visual Studio 2017 无法连接到Web服务器"IIS Express"

    .net core2.2 无法连接到Web服务器"IIS Express" 解决方案: 用命令提示符输入以下命令 sc config http start= auto 重启计算机, ...

  4. STL_map的使用

    转自:http://www.kuqin.com/cpluspluslib/20071231/3265.html Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在m ...

  5. margin与padding如何进行区分

    margin与padding如何进行区分,这是很多学html人的困扰,其实说白了padding 就是内容与边框的空隙.而margin则是模块与模块的空隙.[3]

  6. 简单工厂模式&工厂方法模式&抽象工厂模式的区别

    之前写过一篇关于工厂模式(Factory Pattern)的随笔,里面分析了简单工厂模式,但对于工厂方法和抽象工厂的分析较为简略.这里重新分析分析三者的区别,工厂模式是java设计模式中比较简单的一个 ...

  7. IDEA SpringBoot项目连接数据库报Acess denied错误解决方法

    详见:https://blog.csdn.net/qq_36324464/article/details/79534605

  8. oracle文件 结构01

    1.减少数据的冗余(例如使用id) 2.保证数据库一致性 关联表越多越慢 主机名 hosts 文件 ntp 时间同步

  9. Tomcat 使用redis实现session共享

    准备工作: 1.安装nginx 环境搭建参考:https://blog.csdn.net/fd2025/article/details/79878326 nginx.conf的编辑: 2.同一台机器配 ...

  10. Linux 安装 Tomcat 详解

    说明:安装的 tomcat 为解压版(即免安装版):apache-tomcat-8.5.15.tar.gz (1)使用 root 用户登录虚拟机,在根目录下的 opt 文件夹新建一个 software ...