LeetCode_67. Add Binary
67. Add Binary
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = "11", b = "1"
Output: "100"
Example 2:
Input: a = "1010", b = "1011"
Output: "10101"
package leetcode.easy;
public class AddBinary {
@org.junit.Test
public void test() {
String a1 = "11";
String b1 = "1";
String a2 = "1010";
String b2 = "1011";
System.out.println(addBinary(a1, b1));
System.out.println(addBinary(a2, b2));
}
public String addBinary(String a, String b) {
StringBuffer buffer = new StringBuffer();
int sum = 0;
int carry = 0;
for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; i--, j--) {
sum = carry;
if (i >= 0) {
sum += a.charAt(i) - '0';
}
if (j >= 0) {
sum += b.charAt(j) - '0';
}
buffer.append(sum % 2);
carry = sum / 2;
}
if (carry != 0) {
buffer.append(carry);
}
return buffer.reverse().toString();
}
}
LeetCode_67. 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 ...
- Add Binary
Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...
- 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 ...
随机推荐
- php多版本配置
需求分析: 需要在一台装有php5.4的测试服务器跑的上php7.2.x的项目 安装phpenv(php版本控制) $ sudo yum install git $ mkdir -p repos/gi ...
- 多任务4---greenlet完成多任务
同yield一样 ,单线程,来回切换完成多任务,需要安装greenlet插件 pip install greenlet 代码: from greenlet import greenlet import ...
- C# 调用cmd执行命令
private void CmdRun_Click(object sender, EventArgs e) { Process p = new Process(); ...
- java对象转换
对象转换: 对象的分层涉及到各个层级之间的对象转换(Entity2DTO , DTO2VO, VO2DTO,DTO2Entity等),传统的采用set/get 方法硬编码实现写的代码比较多:或者采用B ...
- 【安卓高级】ViewPager视差动画效果
在安卓开发中,是否遇见过一些很酷的视差动画效果,当ViewPager滑动下一页的时候,页面内的各种元素也能跟随滑动做位移效果,整体看起来非常有活力. 关键的PageTransformer PageTr ...
- Codeforces Round #459 (Div. 2) C题【思维好题--括号匹配问题】
题意:给出一个串,只包含 ( ? ) 三种符号,求出有多少个子串是完美匹配的. ( ) ? ) => ( ) ( ) 完美匹配( ( ) ? => ( ( ) )完美匹配? ? ? ? = ...
- 洛谷 P2038 无线网络发射器选址 题解
每日一题 day9 打卡 Analysis 这道题是个模拟,两个0~128( 注意不是1~128 )的循环枚举正方形中心点,判断正方形的边界,再用循环枚举公共场所的数量就好了. 时间复杂度 < ...
- 基于评分的商品top-N推荐系统
import io # needed because of weird encoding of u.item file import os from surprise import KNNBaseli ...
- Mysql远程无法连接
#登陆mysql $ mysql -uroot -p mysql> use mysql; mysql> update user set host = '%' where user = 'r ...
- sql 查出相同的记录 并把相同记录 显示在一起
select c.workunit unitname,a.positionid,a.positiontype,a.isfirst,a.mastersort,a.directoraudit, c.wri ...