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 ...
随机推荐
- Customize the SharePoint 2013 search experience with a Content Enrichment web service
Did you ever wish you had more control over how your content is indexed and presented as search resu ...
- 【锋利的jQuery】学习笔记03
第三章 jQuery中的DOM操作 一.DOM操作的分类 DOM(document object model)是一种与浏览器.平台.语言无关的接口,使用该接口可以访问页面中的·所有组件.DOM的操作可 ...
- ionic 项目分享【转】No.3
写在文章前:由于最近研究ionic框架,深感这块的Demo寥寥可数,而大家又都藏私,堂堂天朝,何时才有百家争鸣之象,开源精神吾辈当仁不让! 原文地址暂时忘记了 ,如果有知道的麻烦在评论处帮忙说一下 , ...
- Attribute 特性
转载 不错 摘要:纠结地说,这应该算是一篇关于Attribute 的笔记,其中的一些思路和代码借鉴了他人的文笔(见本文底部链接).但是,由于此文对Attribute 的讲解实在是叫好(自夸一下 ...
- rac中 kull session会话脚本
方法:ALTER SYSTEM KILL SESSION '80, 6, @2'; --<= 80 sid,6 serial#,@2 inst_id kill session 脚本如下:sel ...
- 解决WEB(apache)服务器time_wait过高的性能优化过程
目录 1.网站的硬件环境 2.修改Httpd.conf 3.修改sysctl.conf文件 一.网站环境LAMP硬件环境 [root@www conf]# dmidecode -s processor ...
- c++相关知识回顾
1.typedef typedef用来定义同类型的同义词.如: typedef unsingned int size_t; typedef int ptrdiff_t; typedef T * ite ...
- Bad owner or permissions on .ssh/config
Bad owner or permissions on $HOME/.ssh/config The ssh with RHEL 4 is a lot more anal about security ...
- Island of Survival 概率
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...
- Rendering Transparent 3D Surfaces in WPF with C#(转载)
Rendering Transparent 3D Surfaces in WPF with C# The primary problems that arise when rendering semi ...