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. asp.net mvc中包含webapi时,token失效产生302的解决方案

    public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOpt ...

  2. URL与图像格式

    绝对/相对URL “绝对URL”是指资源的完整的地址,通常以“http://”打头: “相对URL”是指Internet上资源相对于当前页面的地址,它包含从当前页面指向目标资源位置的路径,不以“htt ...

  3. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

  4. 【C#进阶系列】06 类型和成员基础

    这些东西是基础中的基础,基本上是本书都会讲这个.但是很多东西到处都有,所以只捡了以下的这些写下来. 关于类型的可见性和可访问性 也就是public,internal这种东西,但是还是有个东西要提一下, ...

  5. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  6. flask-uploads扩展的使用笔记

    涉及的flask扩展 flask-uploads flask的一个文件上传扩展, 提供了UploadSet这个概念 flask-wtf(中文) 很强大的表单的扩展 flask-bootstrap bo ...

  7. <s:iterator>各种遍历用法

    struts2<S:iterator>遍历map小结 1.MapAction.java import java.util.ArrayList;   import java.util.Has ...

  8. Oracle 中 call 和 exec的区别

    今天发现了一个小东西,觉得很有意思,查找了一些资料,跟大家分享一下: 在sqlplus中: 在第三方提供的工具(如:plsqldev) 总结: exec是sqlplus的命令,只能在sqlplus中使 ...

  9. ALV要特别小心的一些地方

    1.在ALV报表里面如果你做了一个字段可编辑而且这个字段是带有小数的数量或者金额,这时候当你输入一个数字保存或者去操作的时候发现他的值会变掉,这个要在设置列属性的时候给他对应的参考表和字段L_FIEL ...

  10. SharePoint通过stsadm备份和还原子网站(不是网站集)

    大家都知道SharePoint的stsadm命令提供了很多便捷甚至是唯一的操作方法! 这里列出的所有命令:http://www.cnblogs.com/dadongzuo/archive/2013/0 ...