当前位置:网站首页>LeetCode琅琊榜第二十层-二进制求和
LeetCode琅琊榜第二十层-二进制求和
2022-06-10 08:43:00 【爪哇土著、JOElib】
LeetCode琅琊榜第二十层-二进制求和
题目内容
二进制计算规则的复习
- 在引出二进制的计算规则之前,不妨再复习一下十进制的计算规则
- 逢十进一,借一当十
- 二进制的计算规则
- 逢二进一,借一当二
示例分析
输入 a = "11",b = "1"
输出 "100"
解析:
我们不妨把他们两个的长度补成一致,即"011"与"001"
1) 1 与 1 与 对应进位相加 = 10 => 这个位上面的结果是0,进位是1[注意,一开始的进位为0]
2) 1 与 0 与 对应进位相加 = 10 => 这个位置上的结果是0,进位是1
3) 0 与 0 与 对应进位相加 = 1 => 这个位置上的结果为1,进位为0
输入 a = "11",b = "1"
输出 "100"
解析:
我们不妨把他们两个的长度补成一致,即"011"与"001"
1) 1 与 1 与 对应进位相加 = 10 => 这个位上面的结果是0,进位是1[注意,一开始的进位为0]
2) 1 与 0 与 对应进位相加 = 10 => 这个位置上的结果是0,进位是1
3) 0 与 0 与 对应进位相加 = 1 => 这个位置上的结果为1,进位为0
输入 a = "1010",b = "1011"
输出 "10101"
解析:
我们不妨把他们两个的长度补成一致,即"01010"与"01011"
1) 1 与 0 与 对应进位相加 = 1 => 这个位上面的结果是1,进位是0[注意,一开始的进位为0]
2) 1 与 1 与 对应进位相加 = 10 => 这个位上面的结果是0,进位是1
3) 0 与 0 与 对应进位相加 = 1 => 这个位上面的结果是1,进位是0
4) 1 与 1 与 对应进位相加 = 10 => 这个位上面的结果是0,进位是1
5) 0 与 0 与 对应进位相加 = 1 => 这个位上面的结果是1,进位是0
算法思想的引出
- 我们发现每一位相加都是对应两个数字与对应的进位相加而得到对应的结果
- 将上面所有的结果拼接起来,然后再将他们翻转过来就是最终索要的答案
代码实现与分析
class Solution {
public String addBinary(String a, String b) {
// carry 该变量用于记录进位的大小
var carry = 0;
// len 该变量用于记录较长的字符串的长度,作用请看解析F
var len = Math.max(a.length(),b.length());
// res 该变量用于拼接结果
var res = new StringBuilder();
// i的初始化,作用请看解析S
for (int i = 0; i < len; i++) {
// 代码思想请看解析T
carry += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
carry += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
res.append((char) (carry % 2 + '0'));
carry /= 2;
}
// 如果超过了最长长度,还有进位,即示例2,我们需要再拿一个位补1
if (carry > 0) {
res.append("1");
}
// 将结果翻转即可
return res.reverse().toString();
}
}
解析:
解析F
- 在示例分析中提出了一个重要的操作,就是要将他们两个的字符串长度补成一样长,用
len记录较长的字符串长度,就可以实现将他们两个字符串的长度补成一致
解析X
- 首先,我们先明确,我们的算法目的是先从低位算起,然后再逐步推向高位的,即低位 -> 高位
- 其次,我们为什么不让
i = len - 1呢?- 如果我们让
i = len - 1,如果有一个较短的字符串会直接越界 - 而且对于字符串边界的情况的判断会变得更加复杂
- 如果我们让
- 我们为什么让
i = 0- 从
i < a.length()或i < b.length()可以直接看出是否越界的情况
- 从
解析T
- 如果
i < a.length,说明没有越界,由于carry是int类型,所以我们对其取到的字符-'0'取到其对应的整数数字,将整数数字叠加到carry - 如果
i > a.length,说明越界了,根据示例,我们需要在不满长度的地方补够对应的长度,补的地方数字为0,所以直接将0叠加到carry即可 - 同理b
- 将
carry%2 + '0'的结果强制类型转换成char添加即可- 为什么强制类型转换?
- 为了得到一个整数数字对应的字符,而不是添加一个整数数字
- 为什么
%2- 因为二进制的取值范围是
{0,1},所以要%2
- 因为二进制的取值范围是
- 为什么强制类型转换?
- 将
carry/=2- 为了拿到进位的结果
- 比如
1 + 1 = 10 进位 = 1,换算成10进制是 1 + 1 = 2 , 2 / 2 = 1,刚好拿到其进位
- 比如
- 为了拿到进位的结果
算法总结
该算法被称为模拟法,是官方解法的法一,法二涉及到位运算,待我把位运算的前置算法题目都刷完,再给大家带来这道题的第二种解法
边栏推荐
- vtk学习之RenderCylinder-Lights灯光渲染
- Simple operation and debugging of GPIO in Qualcomm platform
- Task04:集合运算
- YAML基本语法
- The R language catools package divides the data, the scale function scales the data, the KNN function of the class package constructs a k-nearest neighbor classifier, and compares the model accuracy a
- Preparation computer database mysql +php the next day
- Task06:秋招秘籍 C
- The R language uses the PDF function to save the visual image results to the PDF file, uses the PDF function to open the image device, and uses the dev.off function to close the image device
- R语言e1071包的naiveBayes函数构建朴素贝叶斯模型、使用caret包的confusionMatrix函数计算混淆矩阵(kappa、置信区间、每个类别的评估指标、特异度、敏感度等)
- Hospital blood bank management system source code blood bank source code blood bank management source code hospital source code
猜你喜欢

window11 无法打开安全中心解决方法

Test preparation computer level 2 database day 5

Practice of high performance graph computing system Plato in Nebula graph

Ten working principles for STM32 MPU developers

MMSegmention系列之六(训练技巧)

How to use mind mapping to design test cases

零基础转行软件测试需要学到什么程度才能找工作

What happens when your Huaqiangbei earphone falls into the water? How to restore sound quality?

wechat_ Configuration of wechat applet subcontracting

Computer level 2 test preparation MySQL day 4
随机推荐
樂鑫推出 ESP32-C3 的 AWS IoT 參考示例
wechat_微信小程序分包的配置
A must visit museum in London recommended by: London Museum of natural history
光流法浅学
Web 3: a new era of Internet development
YAML基本语法
Chapter 2 data representation and operation
伦敦旅游必去博物馆推荐:伦敦自然历史博物馆
Add cache like silk to optimize service
泰國曼穀大城府被福布斯評為“後疫情時代最值得一去的城市”
Mmsegment Series IV (custom dataset)
Auto.js pro 开发环境配置
[DRM Audio Converter] noteburner iTunes DRM audio converter download
MMSegmentation系列之模型训练与推理(二)
vtk学习之RenderCylinder-Lights灯光渲染
The R language uses the select function of the dplyr package to customize and change the order of two data columns in the dataframe data
How to make internal interfaces visible to MOQ- How to do internal interfaces visible for Moq?
MMSegmention系列之四(自定义数据集)
第2章 数据的表示和运算
UART中的硬件流控RTS与CTS