当前位置:网站首页>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
边栏推荐
- What is the process of building a website
- Spectrum analysis of ADC sampling sequence based on stm32
- Initial experience | purchase and activate typora software
- One article deals with the microstructure and instructions of class
- Element positioning of Web Automation
- Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
- 东南亚电商指南,卖家如何布局东南亚市场?
- Nacos 的安装与服务的注册
- Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
- Three. Js-01 getting started
猜你喜欢

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]

MoCo: Momentum Contrast for Unsupervised Visual Representation Learning

Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
![Development specification: interface unified return value format [resend]](/img/3e/8751b818147cabbe22e4ce44af7d24.jpg)
Development specification: interface unified return value format [resend]

Dynamic memory management (malloc/calloc/realloc)

Non rigid / flexible point cloud ICP registration

Arduino measures AC current

Matlab smooth curve connection scatter diagram

LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
随机推荐
Starting from 1.5, build a micro Service Framework -- log tracking traceid
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
Go语言实现原理——锁实现原理
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
二叉树递归套路总结
Selenium+pytest automated test framework practice
【原创】程序员团队管理的核心是什么?
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
openresty ngx_lua正則錶達式
Spectrum analysis of ADC sampling sequence based on stm32
6-axis and 9-axis IMU attitude estimation
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
Error when LabVIEW opens Ni instance finder
Tensor attribute statistics
Non rigid / flexible point cloud ICP registration
Déterminer si un arbre binaire est un arbre binaire complet
数据库基础知识(面试)
SPSS analysis of employment problems of college graduates
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share