Add Binary

https://leetcode.com/problems/add-binary/

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

For example,

a = "11"

b = "1"

Return "100".

算法

  1. 假设aLen表示string a的长度,那么它的第aLen - 1个元素表示的最低位;string b同
  2. 用一个flag记录来代表进位

代码

public class Solution {
public String addBinary(String a, String b) {
if (a == null || b == null) {
return "";
}
if (a.isEmpty()) {
return b;
}
if (b.isEmpty()) {
return a;
} int s;
int flag = 0;
StringBuilder sb = new StringBuilder();
int i = a.length() - 1;
int j = b.length() - 1;
do {
if (i >= 0 && j >= 0) {
s = a.charAt(i--) - '0' + b.charAt(j--) - '0' + flag * 1;
} else if (i >= 0) {
s = a.charAt(i--) - '0' + flag * 1;
} else {
s = b.charAt(j--) - '0' + flag * 1;
} flag = s / 2;
sb.append(s % 2);
} while (i >= 0 || j >= 0); if (1 == flag) {
sb.append(1);
} return sb.reverse().toString();
}
}

Add Binary的更多相关文章

  1. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

  2. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  3. LeetCode 面试:Add Binary

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

  4. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  5. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  6. 2016.6.21——Add Binary

    Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...

  7. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  8. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

  9. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

随机推荐

  1. C#设计模式——桥接模式(Bridge Pattern)

    一.概述在软件开发中,我们有时候会遇上一个对象具有多个变化维度.比如对汽车对象来说,可能存在不同的汽车类型,如公共汽车.轿车等,也可能存在不同的发动机,如汽油发动机.柴油发动机等.对这类对象,可应用桥 ...

  2. out 和 ref 参数修饰符

    整理自MSDN out: out 关键字通过引用传递参数.这与 ref 关键字相似,只不过 ref 要求在传递之前初始化变量.若要使用 out 参数,方法定义和调用方法均必须显式使用 out 关键字. ...

  3. 基于吉日嘎底层架构的Web端权限管理操作演示-角色管理

    上一篇介绍了用户管理,这篇来介绍角色管理,这是权限管理的核心部分,因为我们的权限管理系统是基于角色的,有个高大上的英文名叫RBAC(Role Based Acccess Control). 下面的这段 ...

  4. jython 2.7 b3发布

    Jython 2.7b3 Bugs Fixed - [ 2108 ] Cannot set attribute to instances of AST/PythonTree (blocks pyfla ...

  5. EffectiveJava——用函数对象表示策略

    有些语言支持函数指针.代理.lambda表达式,或者支持类似的机制,允许程序把“调用特殊函数的能力”储存起来并传递这种能力.这种机制通常用于允许函数的调用者通过传入第二个函数,来指定自己的行为.比较器 ...

  6. 用C语言制作爱心

    国庆我们实验室布置了作业,其中一项为,利用流程控制语句打印以下图形 * *** ***** ******* ***** *** * 代码如下 #include <stdio.h> int ...

  7. Linux Shell系列教程之(十七) Shell文件包含

    本文是Linux Shell系列教程的第(十七)篇,更多Linux Shell教程请看:Linux Shell系列教程 通过文件包含,可以引用其他文件的内容,也可以将复杂内容分开,使程序结构更加清晰. ...

  8. Metronic 使用到的开源插件汇总

    Metronic 是一套完整的 UI 模板,但不仅仅是模板,更应该说是一个 UI 框架.它除了提供了大量网页模板,也提供了非常多的 UI 组件,并且应用了众多 jQuery 插件.通过这些资源的整合, ...

  9. Vue表单

    gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson11 一 vue表单 实在是太简单了,直接来个例子 <!DOC ...

  10. ASP.NET控件绑定数据源

    DataList/GridView/Repeater DataSet表示数据集,其中包含表,约束和表之间的关系.与现有数据源的交互通过DataAdapter来控制. 源代码示例: SqlDataAda ...