当前位置:网站首页>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
边栏推荐
- Selenium+pytest automated test framework practice
- Sum of two numbers, sum of three numbers (sort + double pointer)
- fibonacci search
- Thoroughly understand JVM class loading subsystem
- How to quickly understand complex businesses and systematically think about problems?
- Creative mode 1 - single case mode
- CJ mccullem autograph: to dear Portland
- Ultrasonic sensor flash | LEGO eV3 Teaching
- 数学公式截图识别神器Mathpix无限使用教程
- 秒杀系统的设计与实现思路
猜你喜欢

Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)

Selenium+pytest automated test framework practice

Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?

数据库基础知识(面试)

audiopolicy

One article deals with the microstructure and instructions of class

第十七周作业

Week 17 homework

Finally understand what dynamic planning is

Practice of concurrent search
随机推荐
【Note17】PECI(Platform Environment Control Interface)
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Yiwen gets rid of the garbage collector
Getting started stm32--gpio (running lantern) (nanny level)
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
Common JVM tools and optimization strategies
Expectation, variance and covariance
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
Selenium+Pytest自动化测试框架实战
[screen recording] how to record in the OBS area
3:第一章:认识JVM规范2:JVM规范,简介;
UVA – 11637 Garbage Remembering Exam (组合+可能性)
Debian 10 installation configuration
Solution to the packaging problem of asyncsocket long connecting rod
Hcip day 11 (BGP agreement)
Openresty ngx Lua regular expression
Week 17 homework
Matlab smooth curve connection scatter diagram
TypeError: this. getOptions is not a function
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性