当前位置:网站首页>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
边栏推荐
- Yiwen gets rid of the garbage collector
- Masked Autoencoders Are Scalable Vision Learners (MAE)
- Realize reverse proxy client IP transparent transmission
- February 13, 2022 -5- maximum depth of binary tree
- Leecode learning notes
- Three.JS VR看房
- ORB_ SLAM2/3
- 3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
- 3:第一章:认识JVM规范2:JVM规范,简介;
- Fix the memory structure of JVM in one article
猜你喜欢
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
【经典控制理论】自控实验总结
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
【Note17】PECI(Platform Environment Control Interface)
Hcip day 11 (BGP agreement)
openresty ngx_lua请求响应
Three.JS VR看房
SPSS analysis of employment problems of college graduates
Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
随机推荐
leecode-学习笔记
证明 poj 1014 模优化修剪,部分递归 有错误
[screen recording] how to record in the OBS area
一文搞定垃圾回收器
Common JVM tools and optimization strategies
Judge whether the binary tree is a complete binary tree
Negative sampling
Starting from 1.5, build a micro Service Framework -- log tracking traceid
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
3D reconstruction of point cloud
(4) UART application design and simulation verification 2 - TX module design (stateless machine)
3D point cloud slam
Yiwen gets rid of the garbage collector
February 13, 2022 -5- maximum depth of binary tree
Idea rundashboard window configuration
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
ORB_ SLAM2/3
数学公式截图识别神器Mathpix无限使用教程
Krypton Factor purple book chapter 7 violent solution