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", b = "1011"
Output: "10101"

注意:

StringBuilder.insert(int offset,  char c) 表示将char放入offset这个偏移量

Character.forDigit(int digit, int radix) 返回radix进制下,该digit所代表的char

代码

 class Solution {
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder(); // new a StringBuilder because it will be easy to append char[] arrayA = a.toCharArray();// convert char to array
char[] arrayB = b.toCharArray();// convert char to array int i = a.length()-1;
int j = b.length()-1;
int carry = 0; while(i>=0 || j>=0 || carry >0){
int updateA = i>=0 ? arrayA[i--]-'0':0;
int updateB = j>=0 ? arrayB[j--]-'0':0;
int sum = updateA + updateB + carry;
sb.insert(0, Character.forDigit(sum%2, 10)); // I convert this sum to binary then insert to sb
carry = sum/2; // coz it is binary, carry would be sum/2
}
return sb.toString(); // convert StringBuilder to String
}
}

[leetcode]67. Add Binary 二进制加法的更多相关文章

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

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

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

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

  3. (String) 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 (高精度加法)

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

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

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

  6. 【LeetCode每天一题】Add Binary(二进制加法)

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

  7. [Leetcode] add binary 二进制加法

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

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

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

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

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

随机推荐

  1. 浅析rune数据类型

    学习golang基础的时候,发现有个叫rune的的数据类型,当时不理解这个类型的意义. 查询,官方的解释如下: // rune is an alias for int32 and is equival ...

  2. Java_集合面试题

    Java_集合面试题 0.链表,队列和栈的区别? 链表是一种存储结构,指得是存储时候除了要存储数据元素之外,还要用数据元素一起的另外空间存储数据元素的关系. 队列和栈都是线性表,属于逻辑结构范畴,都是 ...

  3. nginx 信号

    来源:nginx.cn Nginx信号控制 Nginx控制信号 TERM, INT 快速关闭 QUIT 从容关闭 HUP 重新加载,用新的配置开始新的工作进程 USER1 重新打开日志文件 USER2 ...

  4. 【转载】robocopy的用法

    经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...

  5. IIS6.0+win2003部署MVC网站的一些问题

    安装iis,framework环境不谈.MVC网站部署 步骤: 1.为程序新建一个应用程序池(将default的那个程序池作为模板就可以了) 2.web服务扩展一些启用一些必要的服务 3.新建网站 描 ...

  6. Python【每日一问】03

    问:请给出下列代码的执行结果,并解释 a = dict.fromkeys([6, 7, 8], ["testing", {"name": "ken&q ...

  7. 转:通过ASP.Net页面获取域用户名(当前登陆的用户)

    通过ASP.Net页面获取域用户名(当前登陆的用户) 原文地址: https://www.cnblogs.com/fast-michael/archive/2011/03/14/2057954.htm ...

  8. 小程序开发------mpvue开发时间轴

    亲们支持我的新博客哦==>地址(以后更新会尽量在新博客更新,欢迎大家访问加入我的后宫w) ) 效果展示: 技术栈:mpvue demo==> 代码:

  9. TensorFlow模型加载与保存

    我们经常遇到训练时间很长,使用起来就是Weight和Bias.那么如何将训练和测试分开操作呢? TF给出了模型的加载与保存操作,看了网上都是很简单的使用了一下,这里给出一个神经网络的小程序去测试. 本 ...

  10. MySQL查询当天、本周、本月数据语句

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...