LeetCode Add Binary |My Solution
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
Have you been asked this question in an interview?
public class Solution {
public String addBinary(String a, String b) {
if (a == null || a.length() == 0) {
return b;
}
if (b == null || b.length() == 0) {
return a;
}
int addBits = Math.min(a.length(), b.length());
StringBuffer sb = new StringBuffer();
int bitPlus = 0;
for (int i = 0; i < addBits; i++) {
int aBit = a.charAt(a.length() - 1 - i) - '0';
int bBit = b.charAt(b.length() - 1 - i) - '0';
int cur = aBit + bBit + bitPlus;
bitPlus = cur / 2;
cur = cur % 2;
sb.insert(0, String.valueOf(cur));
}
return doOther( a, b, addBits, bitPlus, sb); }
public String doOther(String a, String b, int addBits,int bitPlus,StringBuffer sb) {
if (b.length() > a.length()) {
doOther( b, a, addBits, bitPlus, sb);
} else {
for (int i = addBits; i < a.length();i++)
{
int aBit = a.charAt(a.length() - 1 - i ) - '0';
int cur = aBit + bitPlus;
bitPlus = cur / 2;
cur = cur % 2;
sb.insert(0,String.valueOf(cur));
}
if (bitPlus == 1) {
sb.insert(0,"1");
}
return sb.toString();
}
return sb.toString(); }
}
LeetCode Add Binary |My Solution的更多相关文章
- 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 = "11"b = ...
- [leetcode]Add Binary @ Python
原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [Leetcode] add binary 二进制加法
Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...
- LeetCode——Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- LeetCode Add Binary 两个二进制数相加
class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
随机推荐
- 六十四 asyncio
asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要 ...
- MySQL的数据引擎讲解
一.MySQL的数据引擎讲解 在MySQL数据库中,常用的引擎主要就是2个:Innodb和MyIASM. 1.简单介绍这两种引擎,以及该如何去选择. a.Innodb引擎,Innodb引擎提供了对数据 ...
- 如何在eclipse 中安装 spring IDE
1.先 确定 当前的eclipse 的版本:(步骤如下) 2.下载spring ide(请确定好ecplice 的版本号) http://spring.io/tools/sts/all 最后: 安装 ...
- Storm基本概念以及Topology的并发度
Spouts,流的源头 Spout是Storm里面特有的名词,Stream的源头,通常是从外部数据源读取tuples,并emit到topology Spout可以同时emit多个tupic strea ...
- 2017广西邀请赛 Query on A Tree (可持续化字典树)
Query on A Tree 时间限制: 8 Sec 内存限制: 512 MB提交: 15 解决: 3[提交][状态][讨论版] 题目描述 Monkey A lives on a tree. H ...
- 初见Python<6>:文件读写
1.open函数语法: python通过open函数打开文件,建立程序与文件之间的连接. open函数语法:open(filename[,mode[,buffering]]) 其中filename是指 ...
- 将字符串的编码格式转换为utf-8
方式一: /** * 将字符串的编码格式转换为utf-8 * * @param str * @return Name = new * String(Name.getBytes("ISO-88 ...
- Problem E: 零起点学算法34——3n+1问题
#include<stdio.h> #include<math.h> int main() { int n; n<=pow(,); ; scanf("%d&qu ...
- [转]提示错误 package javax.servlet.jsp does not exist package javax.servletr.jsp.tagext does not exist
你在JAVA servlet容器运行的时候没配置servlet-api.jar,tools.jar,rt.jar,jsp-api.jar的classpath 我的classpath= .;%JAVA_ ...
- 8VC Venture Cup 2016 - Elimination Round C. Block Towers 二分
C. Block Towers 题目连接: http://www.codeforces.com/contest/626/problem/C Description Students in a clas ...