当前位置:网站首页>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
边栏推荐
- leecode-学习笔记
- golang代码检查工具
- Three. JS VR house viewing
- openresty ngx_ Lua regular expression
- Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
- Data analysis - Thinking foreshadowing
- 一文搞定垃圾回收器
- Idea rundashboard window configuration
- Dynamic memory management (malloc/calloc/realloc)
- Design and implementation of secsha system
猜你喜欢

Getting started stm32--gpio (running lantern) (nanny level)

并查集实践

From the perspective of quantitative genetics, why do you get the bride price when you get married

MoCo: Momentum Contrast for Unsupervised Visual Representation Learning

Initial experience | purchase and activate typora software

【Note17】PECI(Platform Environment Control Interface)

audiopolicy

Sum of two numbers, sum of three numbers (sort + double pointer)

Tensor attribute statistics

Thoroughly understand JVM class loading subsystem
随机推荐
Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
Using LNMP to build WordPress sites
Practice of concurrent search
SPSS analysis of employment problems of college graduates
TypeError: this. getOptions is not a function
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
golang代码检查工具
Masked Autoencoders Are Scalable Vision Learners (MAE)
【经典控制理论】自控实验总结
Debian 10 installation configuration
查看网页最后修改时间方法以及原理简介
audiopolicy
regular expression
What is the process of building a website
Alibaba Tianchi SQL training camp task4 learning notes
如何快速理解复杂业务,系统思考问题?
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
3:第一章:认识JVM规范2:JVM规范,简介;
并查集实践