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练习题(一)
背景: 和公司的二位同事一起学习python,本着共同学习.共同成长.资源共享的目标,然后从中学习,三人行必有我师 练习题更新中······ 题目: 输入一个值num,如果 num 大于 10,输出: ...
- pid 及参数调试方法
所谓PID指的是Proportion-Integral-Differential.翻译成中文是比例-积分-微分. 记住两句话: 1.PID是经典控制(使用年代久远) 2.PID是误差控制() 对直流电 ...
- 字符编码,python解释器------总结
目录 1. 编码: 1.字符编码 2. 编码的历史 3. 编码和解码 2. python解释器 解释代码的流程 1. 读取文本到解释器 2. 识别代码(检查语法问题) 3. 往终端打印 1. 编码: ...
- 002_simulink中显示模块中的名字
(一)Display--->Hide Automatic Names(√去掉)
- 004_硬件基础电路_AD各层含义
- mysql更新数据,条件为实时查询出来的数据
--将更新条件保存到临时表里 CREATE TABLE tmp3 AS (SELECT username FROM oa_user WHERE username NOT IN (SELECT user ...
- 34 | 到底可不可以使用join?
在实际生产中,关于 join 语句使用的问题,一般会集中在以下两类: 我们 DBA 不让使用 join,使用 join 有什么问题呢? 如果有两个大小不同的表做 join,应该用哪个表做驱动表呢? 今 ...
- LOJ P10171 牧场的安排 题解
每日一题 day6 打卡 Analysis 状压dp dp[i][j]+=dp[i-1][k]; #include<iostream> #include<cstdio> #in ...
- VS下字符串与数组互相装换
1.分割字符串IdStr为int数组Ids int[] Ids = Array.ConvertAll<string, int>(IdStr.Trim().Split(','), deleg ...
- leetcode解题报告(23):Pascal's Triangle
描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...