当前位置:网站首页>[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
边栏推荐
- MySQL shutdown is slow
- Talking about the startup of Oracle Database
- idea问题记录
- GNSS定位精度指标计算
- [leetcode19] delete the penultimate node in the linked list
- Unity3d, Alibaba cloud server, platform configuration
- Vulnhub target: hacknos_ PLAYER V1.1
- Force buckle 1189 Maximum number of "balloons"
- NovAtel 板卡OEM617D配置步骤记录
- isEmpty 和 isBlank 的用法区别
猜你喜欢
随机推荐
Expected value (EV)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Fairygui character status Popup
Agile development helps me
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
How to reduce the shutdown time of InnoDB database?
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
單片機藍牙無線燒錄
InnoDB dirty page refresh mechanism checkpoint in MySQL
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Meanings and differences of PV, UV, IP, VV, CV
[leetcode19] delete the penultimate node in the linked list
[Offer29] 排序的循环链表
[leetcode622] design circular queue
In 2020, the average salary of IT industry exceeded 170000, ranking first
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
Knowledge system of digital IT practitioners | software development methods -- agile
FairyGUI循環列錶