当前位置:网站首页>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
边栏推荐
- 判斷二叉樹是否為完全二叉樹
- Common JVM tools and optimization strategies
- 数据库基础知识(面试)
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- Arduino measures AC current
- audiopolicy
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- February 13, 2022-4-symmetric binary tree
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- Codeforces Global Round 19
猜你喜欢
一文搞定class的微觀結構和指令
Error when LabVIEW opens Ni instance finder
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
The method and principle of viewing the last modification time of the web page
Finally understand what dynamic planning is
Vcomp110.dll download -vcomp110 What if DLL is lost
Douban scoring applet Part-2
视频标准二三事
YML configuration, binding and injection, verification, unit of bean
Initial experience | purchase and activate typora software
随机推荐
SPSS analysis of employment problems of college graduates
Leecode learning notes
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
[screen recording] how to record in the OBS area
Common model making instructions
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
Element positioning of Web Automation
一文搞定JVM的内存结构
Thoroughly understand JVM class loading subsystem
一文搞定class的微觀結構和指令
CJ mccullem autograph: to dear Portland
Multi view 3D reconstruction
Three. JS VR house viewing
audiopolicy
Leetcode sword finger offer brush questions - day 21
Error when LabVIEW opens Ni instance finder
Vcomp110.dll download -vcomp110 What if DLL is lost
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
Krypton Factor purple book chapter 7 violent solution