当前位置:网站首页>LeetCode——Add Binary
LeetCode——Add Binary
2022-07-05 23:02:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
Given two binary strings, return their sum (also a binary string).
For example, a = "11"
b = "1"
Return "100"
.
求数字字符串的二进制和。
同之前的数组代表数字,两个数组相加一样。仅仅只是进位变成了2.可能两个串的长度不一样,故逆转。从左到右加下去。最后再逆转。
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;//进位
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();
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117531.html原文链接:https://javaforall.cn
边栏推荐
- Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
- Detailed explanation of pointer and array written test of C language
- regular expression
- Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
- 使用rewrite规则实现将所有到a域名的访问rewrite到b域名
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
- 一文搞定JVM常见工具和优化策略
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- One article deals with the microstructure and instructions of class
- Media query: importing resources
猜你喜欢
VOT toolkit environment configuration and use
YML configuration, binding and injection, verification, unit of bean
【Note17】PECI(Platform Environment Control Interface)
Object detection based on impulse neural network
如何快速理解复杂业务,系统思考问题?
Non rigid / flexible point cloud ICP registration
【Note17】PECI(Platform Environment Control Interface)
Fix the memory structure of JVM in one article
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
Registration and skills of hoisting machinery command examination in 2022
随机推荐
Go语言实现原理——锁实现原理
Multi camera stereo calibration
openresty ngx_lua正则表达式
数据库基础知识(面试)
Week 17 homework
Calculating the number of daffodils in C language
一文搞定垃圾回收器
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
终于搞懂什么是动态规划的
一文搞定class的微观结构和指令
Vcomp110.dll download -vcomp110 What if DLL is lost
How to quickly understand complex businesses and systematically think about problems?
Roman numeral to integer
Using LNMP to build WordPress sites
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Masked Autoencoders Are Scalable Vision Learners (MAE)
C Primer Plus Chapter 9 question 9 POW function
C Primer Plus Chapter 9 question 10 binary conversion
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework