当前位置:网站首页>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
边栏推荐
- Arduino measures AC current
- Realize reverse proxy client IP transparent transmission
- d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
- Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
- Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
- 3:第一章:认识JVM规范2:JVM规范,简介;
- What is the process of building a website
- Un article traite de la microstructure et des instructions de la classe
- Leetcode sword finger offer brush questions - day 21
- Practice of concurrent search
猜你喜欢

Finally understand what dynamic planning is

Detailed explanation of pointer and array written test of C language

How to quickly understand complex businesses and systematically think about problems?

Vision Transformer (ViT)

openresty ngx_lua请求响应

Three. JS VR house viewing

Marginal probability and conditional probability

Douban scoring applet Part-2

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

SPSS analysis of employment problems of college graduates
随机推荐
【Note17】PECI(Platform Environment Control Interface)
YML configuration, binding and injection, verification, unit of bean
Finally understand what dynamic planning is
2:第一章:认识JVM规范1:JVM简介;
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
两数之和、三数之和(排序+双指针)
regular expression
openresty ngx_lua正则表达式
Element positioning of Web Automation
Use of grpc interceptor
一文搞定垃圾回收器
(4) UART application design and simulation verification 2 - TX module design (stateless machine)
Three. Js-01 getting started
Hcip day 11 (BGP agreement)
东南亚电商指南,卖家如何布局东南亚市场?
判斷二叉樹是否為完全二叉樹
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
Practice of concurrent search
TypeError: this. getOptions is not a function