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. [Java] Thread -- 避免Race Condition (Synchronized)

    public class TestRaceCondition implements Runnable{ public static int counter = 0; public static voi ...

  2. zabbix之 自定义内存使用率监控报警

    配置zabbix当内存剩余不足15%的时候触发报警   zabbix默认的剩余内存报警:Average Lack of available memory on server {HOST.NAME}{T ...

  3. Python历史与安装

    1.Python发展历史 起源 Python的作者,Guido von Rossum,荷兰人.1982年,Guido从阿姆斯特丹大学获得了数学和计算机硕士学位.然而,尽管他算得上是一位数学家,但他更加 ...

  4. Mysql 数据库操作之DDL、DML、DQL语句操作

    Mysql 数据库操作之DDL.DML.DQL语句操作 设置数据库用户名密码 l  Show databases 查看数据库列表信息 l  查看数据库中的数据表信息 ,格式: use 数据库名: sh ...

  5. dependency walker检查dll依赖关系目录设置的问题

    废话少说,直接上图 图中来看,似乎IESHIMS.DLL文件不存在报错,实际是因为没有加载IESHIMS.DLL所在的路径. 在我的电脑里面搜索有两个同名的dll,一个是32位的,一个是64位的. C ...

  6. weblogic发序列化命令执行漏洞工具分享

    weblogic发序列化命令执行漏洞工具分享(链接: https://pan.baidu.com/s/1qE5MFJ32672l-MMl-QL-wQ 密码: d85j) JBOSS_EXP 工具分享( ...

  7. [转]IIS 日志记录时间和实际时间 不一样

    今天偶然发现 2003 系统IIS 日志记录时间和实际时间总是差了8个小时,也就是慢了8个小时.苦苦找了半天才发现如下办法能解决 ,特发来分享下 解决1:如果 IIS日志记录默认使用的是W3C扩展日志 ...

  8. 彻底征服 Spring AOP 之 实战篇

      Spring AOP 实战 看了上面这么多的理论知识, 不知道大家有没有觉得枯燥哈. 不过不要急, 俗话说理论是实践的基础, 对 Spring AOP 有了基本的理论认识后, 我们来看一下下面几个 ...

  9. Spring AOP的底层实现原理

    Spring的两大核心之一就是AOP,AOP:面向切面编程.在说原理之前,得先知道一些 AOP的专业术语. AOP的专业术语 连接点(JoinPoint):增强执行的位置(增加代码的位置),Sprin ...

  10. Android 开发 VectorDrawable 矢量图 (二)了解矢量图属性与绘制

    VectorDrawable 矢量图 三部曲: Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图 Android 开发 VectorDrawabl ...