当前位置:网站首页>[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
边栏推荐
- [算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
- Unity3D制作注册登录界面,并实现场景跳转
- wsl常用命令
- KF UD分解之UD分解基础篇【1】
- 服务未正常关闭导致端口被占用
- [leetcode15] sum of three numbers
- Fairygui gain buff value change display
- idea问题记录
- (3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
- FGUI工程打包发布&导入Unity&将UI显示出来的方式
猜你喜欢
Office prompts that your license is not genuine pop-up box solution
FGUI工程打包发布&导入Unity&将UI显示出来的方式
ORA-02030: can only select from fixed tables/views
【干货】提升RTK模糊度固定率的建议之周跳探测
idea中好用的快捷键
FairyGUI循環列錶
Theoretical derivation of support vector machine
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
使用rtknavi进行RT-PPP测试
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
随机推荐
[Chongqing Guangdong education] Shandong University College Physics reference materials
MySQL error warning: a long semaphore wait
音乐播放(Toggle && PlayerPrefs)
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
【RTKLIB 2.4.3 b34 】版本更新简介一
[offer29] sorted circular linked list
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
Latex learning
First use of dosbox
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
第一人称视角的角色移动
GPS高程拟合抗差中误差的求取代码实现
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
2021.11.10汇编考试
Excel导入,导出功能实现
程序设计大作业:教务管理系统(C语言)
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
地球围绕太阳转
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)