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 ...
随机推荐
- 【转】Python源码学习Schedule
原文:https://www.cnblogs.com/angrycode/p/11433283.html ----------------------------------------------- ...
- region特征
一: 查看阈值之后的region特征,可以通过特征检测来看,在工具栏上 region特征分三部分: 1.基础特征: region面积,中心,宽高,左上角及右下角坐标,长半轴短半轴椭圆方向,洞数及其面积 ...
- vue1 监听数据变化
- Selenium常用API的使用java语言之19-调用JavaScript代码
虽然WebDriver提供了操作浏览器的前进和后退方法,但对于浏览器滚动条并没有提供相应的操作方法.在这种情况下,就可以借助JavaScript来控制浏览器的滚动条.WebDriver提供了execu ...
- Luogu P2148 [SDOI2009]E&D (sg函数 博弈)
题目 洛谷传送门 题解 打表找sgsgsg规律. 严谨证明见:纳尔的博客 CODE #include <bits/stdc++.h> using namespace std; int sg ...
- Second Max of Array
Find the second max number in a given array. Example Given [1, 3, 2, 4], return 3. Given [1, 2], ret ...
- ajax的使用方法
后台在写代码时 一般都会用到AJAX传值的方法 了解的AJAX方法有三种样式 第一 $.ajax( { type: "POST", url: "UserList.ashx ...
- ORA-00600[2662]问题 汇总
一.ORA-00600[2662]问题模拟及解决方法 这是2013年的一篇文章,也不知道数据库是什么版本, 我的数据库时11.2.0.4,使用下面方法模拟的时候,模拟不出来.... 参照eygle ...
- openssl制作双向认证经过验证可行
openssl制作双向认证经过验证可行 http://www.360doc.com/content/12/0524/15/2150778_213390447.shtml 2012-05-24 履历馆 ...
- CF757F Team Rocket Rises Again——最短路+支配树
CF757F Team Rocket Rises Again 全体起立,全体起立,这是我A的第一道黑题(虽然是CF的): 来一波番茄攻击: 不扯淡了,这道题也是学习支配树(之前)应该做的题: 和灾难不 ...