当前位置:网站首页>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
边栏推荐
- Simple and beautiful method of PPT color matching
- Three.js-01 入门
- fibonacci search
- Krypton Factor purple book chapter 7 violent solution
- Matlab smooth curve connection scatter diagram
- TOPSIS code part of good and bad solution distance method
- Negative sampling
- The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
- Go language implementation principle -- lock implementation principle
- golang代码检查工具
猜你喜欢

Use of grpc interceptor

视频标准二三事
![[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]](/img/b4/af689abb3ad4e25988f2d17152406e.jpg)
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]

Realize reverse proxy client IP transparent transmission

Element operation and element waiting in Web Automation

Common JVM tools and optimization strategies

Simple and beautiful method of PPT color matching

2: Chapter 1: understanding JVM specification 1: introduction to JVM;

Marginal probability and conditional probability

Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
随机推荐
openresty ngx_lua正則錶達式
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
秒杀系统的设计与实现思路
February 13, 2022-4-symmetric binary tree
C Primer Plus Chapter 9 question 9 POW function
Selenium+Pytest自动化测试框架实战
Three. Js-01 getting started
C Primer Plus Chapter 9 question 10 binary conversion
Realize reverse proxy client IP transparent transmission
Debian 10 installation configuration
Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
regular expression
UVA – 11637 Garbage Remembering Exam (组合+可能性)
查看网页最后修改时间方法以及原理简介
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
Dynamic memory management (malloc/calloc/realloc)
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Use of shell:for loop