Add Binary
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".
算法
- 假设aLen表示string a的长度,那么它的第aLen - 1个元素表示的最低位;string b同
- 用一个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的更多相关文章
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 2016.6.21——Add Binary
Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
随机推荐
- JS Date.Format
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- 【iOS】XcodeColors插件与CocoaLumberjack工具
工欲善其事必先利其器,好的开发者一定是懂得利用工具来提高自己的效率的,Xcode有很多第三方插件可以使用,最近发现一个可以给控制台着色的工具XcodeColors,结合CocoaLumberjack一 ...
- 重新想象 Windows 8 Store Apps (41) - 打印
[源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...
- Oracle数据库导入导出总结(dmp文件)
Oracle 10G 管理页面(Oracle Enterprise Manager 10g): http://localhost:1158/em http://localhost:1158/em/co ...
- HTML5实现屏幕手势解锁(转载)
来源:https://github.com/lvming6816077/H5lockhttp://threejs.org/examples/http://www.inf.usi.ch/phd/wett ...
- [PE结构分析] 6.IMAGE_SECTION_HEADER
IMAGE_SECTION_HEADER 的源代码如下: typedef struct _IMAGE_SECTION_HEADER { BYTE Name[IMAGE_SIZEOF_SHORT_NAM ...
- IntelliJ和tomcat中的目录结构
IntelliJ和tomcat中的目录结构 IntelliJ的官网帮助中心:http://www.jetbrains.com/idea/webhelp/getting-help.html pr ...
- CSS的一些小事
1.什么时候能将零散的图片整合成一张大图,达到减少请求数的作用? 答:整合进大图的图片是被设置no-repeat用的,如果是repeat-x.repeat-y就不可以. 2.E + F 选择紧贴在E元 ...
- SQLSERVER数据库表各种同步技术
1 --SQLSERVER数据库表各种同步技术 减少SQLServer中每次的同步数据量 2 3 --说到数据库,我就不由地想到同步数据,如何尽可能地减少每次的同步数据量,以此来提高同步效率,降低对网 ...
- RHEL7文件归档与压缩
本文介绍RHEL7.2文件的归档和压缩 文件归档 归档的好处:方便使用.查询.阅读,易于管理 (批量删除文件) 常用操作 命令:tar 作用:将许多文件一起保存至一个单独的磁带或磁盘归档,并能从归档中 ...