当前位置:网站首页>Leetcode Langya list level 20 - binary sum
Leetcode Langya list level 20 - binary sum
2022-06-10 08:53:00 【Javanese aborigines, joelib】
LeetCode The 20th floor of Langya list - Binary sum
List of articles
Topic content
Review of binary calculation rules
- Before exporting binary calculation rules , We might as well review the rules of decimal calculation
- Dot into one , Borrow one for ten
- Binary calculation rules
- On the two into one , Borrow one for two
Sample analysis
Input a = "11",b = "1"
Output "100"
analysis :
We might as well make up the length of both of them , namely "011" And "001"
1) 1 And 1 And Corresponding carry addition = 10 => The result above this bit is 0, Carry is 1[ Be careful , The initial carry is 0]
2) 1 And 0 And Corresponding carry addition = 10 => The result in this position is 0, Carry is 1
3) 0 And 0 And Corresponding carry addition = 1 => The result at this position is 1, Carry is 0
Input a = "11",b = "1"
Output "100"
analysis :
We might as well make up the length of both of them , namely "011" And "001"
1) 1 And 1 And Corresponding carry addition = 10 => The result above this bit is 0, Carry is 1[ Be careful , The initial carry is 0]
2) 1 And 0 And Corresponding carry addition = 10 => The result in this position is 0, Carry is 1
3) 0 And 0 And Corresponding carry addition = 1 => The result at this position is 1, Carry is 0
Input a = "1010",b = "1011"
Output "10101"
analysis :
We might as well make up the length of both of them , namely "01010" And "01011"
1) 1 And 0 And Corresponding carry addition = 1 => The result above this bit is 1, Carry is 0[ Be careful , The initial carry is 0]
2) 1 And 1 And Corresponding carry addition = 10 => The result above this bit is 0, Carry is 1
3) 0 And 0 And Corresponding carry addition = 1 => The result above this bit is 1, Carry is 0
4) 1 And 1 And Corresponding carry addition = 10 => The result above this bit is 0, Carry is 1
5) 0 And 0 And Corresponding carry addition = 1 => The result above this bit is 1, Carry is 0
The derivation of algorithm thought
- We find that every bit adds up to Add the corresponding two numbers to the corresponding carry to get the corresponding result
- Put all the above results together , And then they Flip Come here is the final answer
Code implementation and Analysis
class Solution {
public String addBinary(String a, String b) {
// carry This variable is used to record the size of the carry
var carry = 0;
// len This variable is used to record the length of a long string , See analysis for the function F
var len = Math.max(a.length(),b.length());
// res This variable is used to splice the results
var res = new StringBuilder();
// i The initialization , See analysis for the function S
for (int i = 0; i < len; i++) {
// See parsing for code ideas 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;
}
// If the maximum length is exceeded , And carry , I.e. example 2, We need to take another place 1
if (carry > 0) {
res.append("1");
}
// Just flip the result
return res.reverse().toString();
}
}
analysis :
analysis F
- An important operation is proposed in the example analysis , Is to make up the length of their two strings to be the same , use
lenRecord long string length , You can complement the length of their two strings to be consistent
analysis X
- First , Let's make it clear first , Our algorithm aims to start from the low order , And then gradually push to the high position , namely Low position -> High position
- secondly , Why don't we let
i = len - 1Well ?- If we let
i = len - 1, If there is a short string, it will directly cross the boundary - Moreover, the judgment of string boundary will become more complicated
- If we let
- Why do we let
i = 0- from
i < a.length()ori < b.length()It can be directly seen whether the boundary is crossed
- from
analysis T
- If
i < a.length, It means there is no cross-border , becausecarryyesinttype , So the characters we get from it-'0'Get its corresponding integer number , Superimpose integer numbers oncarry - If
i > a.length, It means that the border is crossed , According to the example , We need to make up the corresponding length where the length is insufficient , The figures to be added are 0, So directly 0 Overlay tocarrythat will do - Empathy b
- take
carry%2 + '0'The result of is cast tocharCan be added to- Why cast ?
- To get a character corresponding to an integer number , Instead of adding an integer number
- Why?
%2- Because the binary value range is
{0,1}, So we need to%2
- Because the binary value range is
- Why cast ?
- take
carry/=2- In order to get the result of rounding
- such as
1 + 1 = 10 carry = 1, The conversion 10 Into the system is 1 + 1 = 2 , 2 / 2 = 1, Just get its carry
- such as
- In order to get the result of rounding
Algorithm is summarized
This algorithm is called the simulation method , It is the first method of the official solution , Method two involves in place operations , When I have finished the pre algorithm of bit operation , Then I will bring you the second solution to this problem
边栏推荐
- MMSegmention系列之五(自定义模型)
- vtk学习之引用计数与智能指针
- 数据库视图、索引、存储过程、触发器简单创建
- SAAS服务能有哪些优势
- win11下配置vscode+cmake
- 乐鑫 ESP RainMaker 加速企业智能转型,私有云方案助力客户打造自有品牌
- Model training and reasoning of mmsegmentation series (2)
- R语言epiDisplay包的tab1函数计算向量数据的频率并可视化(一维频率表、频数的百分比、累积的百分比、使用条形图可视化频数分布)、使用cex.lab参数自定义频数条形条形图轴标签文本字体的大小
- texstudio 显示行号和不检查拼写设置
- 解压jar包修改配置文件(解压、修改、压缩、运行)
猜你喜欢
随机推荐
R语言使用lm函数构建简单线性回归模型(建立线性回归模型)、拟合回归直线、使用attributes函数查看线性回归模型的属性信息、获取模型的系数值coefficients
texstudio 显示行号和不检查拼写设置
Actual use of runloop
The tab1 function in the epidisplay package of R language calculates the frequency of vector data and visualizes it (one-dimensional frequency table, frequency percentage, cumulative percentage, using
36氪首发 | 新一代iPOCT产品持续发展,「伊鸿健康 」完成新一轮数千万元融资
MFC窗口增加状态栏的方法
对线HR_MySQL逻辑架构?就这?
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
AWS IOT reference example of Lexin launching esp32-c3
LeetCode琅琊榜第十八层-两数之和(查找表法)
自己炒股怎么开户?安全吗?
Zotero beta 6.0版本安装后,无法使用内置pdf阅读器的问题解决
vscode-markdown all in one-keyboard shortcut
Task04:集合运算
切换vscode的格式化插件
对线HR_MySQL存储引擎,原来是这样啊
uni-app_ Configure network request in wechat applet development project (third-party package @escook/request miniprogram)
HP notebook - how to wake up a laptop after sleep
想转行,为什么首选软件测试?
ifstream seekg( ) read( )文本操作









