当前位置:网站首页>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
边栏推荐
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
- 【Note17】PECI(Platform Environment Control Interface)
- UVA – 11637 Garbage Remembering Exam (组合+可能性)
- 二叉树递归套路总结
- Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
- Judge whether the binary tree is a complete binary tree
- Marginal probability and conditional probability
- fibonacci search
- Element positioning of Web Automation
- Object detection based on impulse neural network
猜你喜欢

Go语言实现原理——Map实现原理

Ultrasonic sensor flash | LEGO eV3 Teaching

视频标准二三事

Element positioning of Web Automation
![[untitled]](/img/8c/607776e79d66acf9282dca127e12e1.jpg)
[untitled]

YML configuration, binding and injection, verification, unit of bean

【经典控制理论】自控实验总结

The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened

PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )

Debian 10 installation configuration
随机推荐
Media query: importing resources
fibonacci search
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Development specification: interface unified return value format [resend]
SPSS analysis of employment problems of college graduates
6-axis and 9-axis IMU attitude estimation
openresty ngx_ Lua request response
Registration and skills of hoisting machinery command examination in 2022
数据库基础知识(面试)
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
Leetcode sword finger offer brush questions - day 21
Non rigid / flexible point cloud ICP registration
Week 17 homework
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
Composition of interface
【Note17】PECI(Platform Environment Control Interface)
Krypton Factor purple book chapter 7 violent solution
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性