LeetCode 面试:Add Binary
1 题目
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
接口
String addBinary(String a, String b)
2 思路
复杂度
3 代码
public String addBinary(String a, String b) {
char[] ac = new StringBuilder(a).reverse().toString().toCharArray();
char[] bc = new StringBuilder(b).reverse().toString().toCharArray();
int alen = ac.length;
int blen = bc.length;
StringBuilder res = new StringBuilder();
int max = alen > blen ? alen : blen;
int carry = 0;
for (int i = 0; i < max; i++) {
int ai = i < alen ? ac[i] - '0' : 0;
int bi = i < blen ? bc[i] - '0' : 0;
int sum = ai + bi + carry;
carry = sum / 2;
res.append((char) (sum % 2 + '0'));
}
if (carry == 1)
res.append('1');
return res.reverse().toString();
}
4 总结
- 关键是处理相加和进位
- 采用StringBuilder reverse()
- 和Add Two Number类似
5 扩展
6 参考
LeetCode 面试:Add Binary的更多相关文章
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- 【leetcode】Add Binary
题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...
- Java for LeetCode 067 Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- LeetCode(56)-Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example, a = "11&quo ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
随机推荐
- Java基础知识强化之网络编程笔记09:TCP之客户端键盘录入服务器写到文本文件中
1. TCP之客户端键盘录入服务器写到文本文件中 (1)客户端: package cn.itcast_09; import java.io.BufferedReader; import java.io ...
- RedHat7安装Tomcat
编译安装Tomcat 下载jdk (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- user.table.column, table.column 或列说明无效
Oracle统计采用别名出错(user.table.column, table.column 或列说明无效) >>>>>>>>>>>& ...
- Python实战:美女图片下载器,海量图片任你下载
Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...
- 使用linq语句获取指定条数的记录
//获得指定个数的子文件夹,用于分页 var pageAlbums = (from SPFolder pf in lstSubAlbums select pf) ...
- sql - 选出指定范围的行
Select no=Identity(int,1,1),* Into #temptable From dbo.tName order by fName --利用Identity函数生成记录序号 Sel ...
- 分享一张oracle scan图
- Delphi 类方法和普通方法的区别 .
//类声明 TMyClass = class public class procedure MyProc; //类方式 constructor Create; //Crea ...
- 中文翻译:pjsip文档(四)之ICE Session的使用方法
1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...
- [Lua]cocos framework
package_support function cc.register(name, package) function cc.load(...) function cc.bind(target, . ...