当前位置:网站首页>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
边栏推荐
- Element positioning of Web Automation
- npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
- 利用LNMP实现wordpress站点搭建
- Realize reverse proxy client IP transparent transmission
- [speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
- Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
- poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
- 判断二叉树是否为完全二叉树
- Three. Js-01 getting started
- Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
猜你喜欢

Expectation, variance and covariance

Finally understand what dynamic planning is

如何快速理解复杂业务,系统思考问题?

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

CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)

【Note17】PECI(Platform Environment Control Interface)

One article deals with the microstructure and instructions of class

Using LNMP to build WordPress sites

Selenium+Pytest自动化测试框架实战
![[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]](/img/03/8fa104b177698a15b7ffa70d4fb524.jpg)
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
随机推荐
asp.net弹出层实例
JVM的简介
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
How to quickly understand complex businesses and systematically think about problems?
regular expression
Debian 10 installation configuration
数学公式截图识别神器Mathpix无限使用教程
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Matlab smooth curve connection scatter diagram
UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
Initial experience | purchase and activate typora software
openresty ngx_ Lua regular expression
一文搞定JVM的内存结构
Development specification: interface unified return value format [resend]
Go language implementation principle -- map implementation principle
两数之和、三数之和(排序+双指针)
[screen recording] how to record in the OBS area
What is the process of building a website