Add Binary
Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

SOLUTION:

指针指到两个字符串的末尾,不断往前推进,用carry表示进位。用stringbuilder来记录结果。

使用insert(0, c)函数将加出的结果不断插入到STRINGBUILDER.

 public class Solution {
public String addBinary(String a, String b) {
if (a == null || b == null) {
return null;
} if (a.length() == 0) {
return b;
} if (b.length() == 0) {
return a;
} StringBuilder sb = new StringBuilder(); int p1 = a.length() - 1;
int p2 = b.length() - 1; int carry = 0;
while (p1 >= 0 || p2 >= 0) {
int sum = carry;
if (p1 >= 0) {
sum += (a.charAt(p1) - '0');
} if (p2 >= 0) {
sum += (b.charAt(p2) - '0');
} char c = sum % 2 == 1 ? '1': '0';
sb.insert(0, c);
carry = sum / 2; p1--;
p2--;
} if (carry == 1) {
sb.insert(0, '1');
} return sb.toString();
}
}

GITHUB CODE:

AddBinary.java

LeetCode: Add Binary 解题报告的更多相关文章

  1. 【LeetCode】67. Add Binary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BigInteger类 模拟加法 日期 题目地址:h ...

  2. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  5. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  6. 【LeetCode】 258. Add Digits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...

  7. 【LeetCode】415. Add Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  8. 【LeetCode】241. Different Ways to Add Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归构建所有表达式 方法二:分而治之 日期 ...

  9. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

随机推荐

  1. MVC日期和其它字符串格式化

    -- (月份位置不是03) string.Format("{0:D}",System.DateTime.Now) 结果为:2009年3月20日 : :: -- : -- :: st ...

  2. 〖Linux〗Ubuntu13.10,在终端打开gvim提示“GLib-GObject-WARNING”的临时解决办法

    今天刚刚升级至Ubuntu13.10,在终端打开gvim时提示一些出错信息,不是很雅观: (gvim:): GLib-GObject-WARNING **: Attempt to add proper ...

  3. extends android.view.ViewGroup两种实现

    /*    private int measureHeight(int heightMeasureSpec) {         int count = getChildCount();        ...

  4. java.lang.NoClassDefFoundError: Could not initialize class com.demo.jdbc.utils.MyJdbcUtils

    jdbc编写工具类的过程中测试失败, 出现如下错误:   原因:初始化的过程中执行静态代码块的过程中出现错误, 也就是说, 加载配置文件错误: 没有加载到指定路径的配置文件. 我的MyJdbcUtil ...

  5. HDUOJ--点球大战

    点球大战 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. SqlServer强制断开数据库已有连接的方法(转)

    在master数据库中执行如下代码 declare @i INT  declare cur cursor for select spid from sysprocesses where db_name ...

  7. eclipse 中使用中文JAVA api文档

    http://hi.baidu.com/danghj/item/7625a1be20946e43ba0e1202在eclipse中使用中文JAVA api文档Sun 官方的中文版 Java API 文 ...

  8. img图片自适应宽和高[转]

    控制缩略图常见的是JS来控制,还有就是最直接的方法定义img的宽高:下面两种方法自适应宽和高,zhenzhai推荐使用CSS方法:一.CSS方法:主 要是在CSS设置最小值和最大值(max-width ...

  9. DataTable.Compute()

    一.DataTable.Compute()方法說明如下 作用:          计算用来传递筛选条件的当前行上的给定表达式. 格式為:          Object Compute (string ...

  10. Mosquitto ---学习笔记

    Mosquitto的安装,可以参见:https://mosquitto.org/download/ [基础配置]Mosquitto服务的配置文件为/etc/mosquitto/mosquitto.co ...