当前位置:网站首页>[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
边栏推荐
- Devops' future: six trends in 2022 and beyond
- VLSM variable length subnet mask partition tips
- Minio file download problem - inputstream:closed
- [offer78] merge multiple ordered linked lists
- [算法] 剑指offer2 golang 面试题10:和为k的子数组
- [算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
- JUC forkjoin and completable future
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
- Prove the time complexity of heap sorting
- 服务未正常关闭导致端口被占用
猜你喜欢
FGUI工程打包发布&导入Unity&将UI显示出来的方式
idea中好用的快捷键
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
Excel导入,导出功能实现
Fairygui gain buff value change display
MySQL shutdown is slow
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
Guided package method in idea
idea中导包方法
FairyGUI增益BUFF數值改變的顯示
随机推荐
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
Prove the time complexity of heap sorting
Database table splitting strategy
Halcon knowledge: gray_ Tophat transform and bottom cap transform
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
It has been solved by personal practice: MySQL row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT
Special palindromes of daily practice of Blue Bridge Cup
Itext 7 生成PDF总结
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
基于rtklib源码进行片上移植的思路分享
341. Flatten nested list iterator
FairyGUI增益BUFF數值改變的顯示
[算法] 剑指offer2 golang 面试题1:整数除法
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
rtklib单点定位spp使用抗差估计遇到的问题及解决
[算法] 剑指offer2 golang 面试题2:二进制加法