当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 2: binary addition
[algorithm] sword finger offer2 golang interview question 2: binary addition
2022-07-06 12:50:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 2: Binary addition
subject 1:
Given two 01 character string a and b , Please calculate their sum , And output... In the form of binary string .
Input is Non empty String and contains only numbers 1 and 0.
Example 1:
Input : a = “11”, b = “10”
Output : “101”
Example 2:
Input : a = “1010”, b = “1011”
Output : “10101”
Tips :
Each string consists only of characters ‘0’ or ‘1’ form .
1 <= a.length, b.length <= 10^4
If the string is not “0” , No leading zeros .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/JFETK5
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas 1: Analog binary addition
Analog binary addition time complexity O(n)
Code
func addBinary(a string, b string) string {
// Ideas : Analog binary computing
length := len(a)
if len(b) > len(a) {
length = len(b)
}
// Construct returns
resBs := make([]byte, length)
// Pointer pointing from back to front
aP, bP := len(a)-1, len(b)-1
// Carry or not
up := 0
for i := len(resBs) - 1; i >= 0; i-- {
var aByte byte = '0'
var bByte byte = '0'
if aP >= 0 {
aByte = a[aP]
aP--
}
if bP >= 0 {
bByte = b[bP]
bP--
}
// Number of Statistics
count1 := 0
if aByte == '1' {
count1++
}
if bByte == '1' {
count1++
}
count1 += up
// Determine the current value and by the number up
if count1 == 0 {
up = 0
resBs[i] = '0'
} else if count1 == 1 {
up = 0
resBs[i] = '1'
} else if count1 == 2 {
up = 1
resBs[i] = '0'
} else {
up = 1
resBs[i] = '1'
}
}
// final up
res := string(resBs)
if up == 1 {
res = "1" + res
}
return res
}
test
边栏推荐
猜你喜欢
Force buckle 1189 Maximum number of "balloons"
Single chip Bluetooth wireless burning
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Unity scene jump and exit
FairyGUI按钮动效的混用
In 2020, the average salary of IT industry exceeded 170000, ranking first
Fabrication d'un sac à dos simple fairygui
Combination of fairygui check box and progress bar
RTKLIB: demo5 b34f.1 vs b33
KF UD分解之UD分解基础篇【1】
随机推荐
Combination of fairygui check box and progress bar
Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
Matlab读取GNSS 观测值o文件代码示例
Office提示您的许可证不是正版弹框解决
rtklib单点定位spp使用抗差估计遇到的问题及解决
2021.11.10汇编考试
音乐播放(Toggle && PlayerPrefs)
Fairygui character status Popup
Prove the time complexity of heap sorting
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
idea问题记录
What are the advantages of using SQL in Excel VBA
Special palindromes of daily practice of Blue Bridge Cup
Mysql database index
FairyGUI简单背包的制作
Unity3d, Alibaba cloud server, platform configuration
Fairygui joystick
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
[leetcode15] sum of three numbers