当前位置:网站首页>LeetCode——Add Binary
LeetCode——Add Binary
2022-07-05 23:16:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
Given two binary strings, return their sum (also a binary string).
For example, a = "11"
b = "1"
Return "100"
.
Find the binary sum of numeric strings .
The same as the previous array represents numbers , The addition of two arrays is the same . Only the carry becomes 2. Maybe the length of the two strings is different , So reverse . Add from left to right . Finally, reverse .
public static String addBinary(String a, String b) {
StringBuilder ar = new StringBuilder(a).reverse();
StringBuilder br = new StringBuilder(b).reverse();
StringBuilder result = new StringBuilder();
int len = Math.max(a.length(), b.length());
int carry = 0;// carry
for (int i = 0; i < len; i++) {
int t1 = (i >= a.length() ? 0 : (ar.charAt(i) - '0'));
int t2 = (i >= b.length() ? 0 : (br.charAt(i) - '0'));
int t3 = t1 + t2 + carry;
carry = t3 / 2;
t3 = t3 % 2;
result.append(t3);
}
if (carry != 0)
result.append(carry);
result.reverse();
return result.toString();
}
Copyright notice : This article is an original blog article . Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117531.html Link to the original text :https://javaforall.cn
边栏推荐
- [screen recording] how to record in the OBS area
- Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
- 基于脉冲神经网络的物体检测
- 两数之和、三数之和(排序+双指针)
- UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
- Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
- Krypton Factor purple book chapter 7 violent solution
- 数学公式截图识别神器Mathpix无限使用教程
- Development specification: interface unified return value format [resend]
- 3 find the greatest common divisor and the least common multiple
猜你喜欢
Registration and skills of hoisting machinery command examination in 2022
YML configuration, binding and injection, verification, unit of bean
audiopolicy
查看网页最后修改时间方法以及原理简介
SPSS analysis of employment problems of college graduates
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
Thoroughly understand JVM class loading subsystem
TypeError: this. getOptions is not a function
Selenium+Pytest自动化测试框架实战
实现反向代理客户端IP透传
随机推荐
两数之和、三数之和(排序+双指针)
From the perspective of quantitative genetics, why do you get the bride price when you get married
February 13, 2022-4-symmetric binary tree
如何快速理解复杂业务,系统思考问题?
一文搞定class的微觀結構和指令
ORB_ SLAM2/3
Hj16 shopping list
Selenium+Pytest自动化测试框架实战
并查集实践
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
leecode-学习笔记
Multi camera stereo calibration
Déterminer si un arbre binaire est un arbre binaire complet
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Hcip day 12 (BGP black hole, anti ring, configuration)
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Initial experience | purchase and activate typora software
Krypton Factor-紫书第七章暴力求解
How to quickly understand complex businesses and systematically think about problems?
Simple and beautiful method of PPT color matching