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 ...
随机推荐
- .Net反射机制分析和使用
1..NET反射的概述 .NET反射是审查元数据并动态收集关于它的类型信息的能力. 应用程序结构分为应用程序域—程序集—模块—类型—成员几个层次,公共语言运行库加载器管理应用程序域.这些域在拥有相同应 ...
- 关于foreach中对集合执行Add或者Remove操作引发枚举值被修改异常
方法传入集合List<string> ids; 执行操作后再次循环引发异常 foreach (string id in ids) { ids.Add("a"); } 更 ...
- win32程序启用控制台
#include <cstdio> #define USE_WIN32_CONSOLE int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _I ...
- javascript的 == 与 === 的区别
1.对于基础类型,例如string,number ==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直 ...
- ACdrea 1217---Cracking' RSA(高斯消元)
ACdrea 1217---高斯消元 Description The following problem is somehow related to the final stage of many ...
- Couchbase介绍,更好的Cache系统
在移动互联网时代,我们面对的是更多的客户端,更低的请求延迟,这当然需要对数据做大量的 Cache 以提高读写速度. 术语 节点:指集群里的一台服务器. 现有 Cache 系统的特点 目前业界使用得最多 ...
- struts2中valueStack,stackContext以及actionContext的关系
一,首先给出三者的定义 1.valueStack: 里面存放的是Action类中通过set方法设置的属性值(表单传过来的值等),由OGNL框架实现; 2.stackContext: 也是用来存值的,s ...
- Design Patterns (简单工厂模式)
文章很长很精彩,如是初学请耐心观看.(大神请绕道!) 简单工厂模式: 1.创建型模式 2.简单工厂模式概述 3.简单工厂模式的结构与实现 4.简单工厂模式的应用实例 5.创建对象与使用对象 6.简单工 ...
- Play 可以做的 5 件很酷的事
Play 可以做的 5 件很酷的事 本章译者:@Playframwork 通过 5 个实例,透视 Play 框架背后的哲学. 绑定 HTTP 参数到 JAVA 方法参数 用 Play 框架,在 Jav ...
- ADODB.Connection 错误 ‘800a0e7a’ 未找到提供程序
问题表现:做网站ASP页面提示:ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. 解决方案:一般都是64位系统的原因,把IIS切换为32Bit模式运 ...