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 ...
随机推荐
- Android(java)学习笔记174:SharedPreferences(轻量级存储类)
1.SharedPreferences是Android平台上一个轻量级的存储类,简单的说就是可以存储一些我们需要的变量信息.2个activity 之间的数据传递除了可以他通过intent来传递数据,还 ...
- wget下载网站整个目录
wget -r -p -np -k -P ./data/ http://example.com/eg/ 具体参数: -P 表示下载到哪个目录 -r 表示递归下载 -np 表示不下载旁站连接 -k 表示 ...
- Linux下安装Python pip
在Python环境下,pip提供类似yum一样的下载方式,比easy_install方便的多. 1.下载get-pip.py wget https://bootstrap.pypa.io/get-pi ...
- Bash判断是否是root
#!/bin/bash ]; then echo "Not Root" exit fi
- Object传入String类型和其他
是可以传入的. package com.sun.test; public class Test03 { /** * @param args */ public static void main(Str ...
- 在Abp框架中使用Mysql数据库的方法以及相关问题小记
最近发现了一款DDD的框架 看起来不错,据说挺流弊的 刚好最近要弄点小东西,拿来试试也不错 苦于穷逼买不起高配服务器,只好装mysql数据库了 下面说下如何在该框架下使用Mysql数据库 打开项目后, ...
- Error parsing XML: not well-formed (invalid token)
从网络上或别的文件复制粘贴进来的代码有隐含格式,可将内容先粘贴进记事本清除格式,再复制粘贴进工程文件,即可解决此问题 注:1. 要使工程文件全选清空, 2. 若粘贴后刷新仍无效果,可手动输入
- FragmentTransaction.addToBackStack无效的问题
FragmentTransaction.addToBackStack无效的问题: 如果当前的类继承的ActionBarActivity,则FragmentManager必须来自v4包,这样addToB ...
- TCP服务器端和客服端(一)
就是一个客服端(Socket)和服务器(ServerSocket)端的链接间.我的理解是一个服务端可以链接多个客服端. 在客服端有输入流outPutStream. 用于发送数据 在服务器端有输出流.i ...
- Hadoop 系列 - (1) - 学习随笔 - 起源、构成
起源:Hadoop是google 的集群系统的开源实现 --Google集群系统,:GFS(Google file system),MapReduce,BigTable(严格意义 ...