当前位置:网站首页>[算法] 劍指offer2 golang 面試題2:二進制加法
[算法] 劍指offer2 golang 面試題2:二進制加法
2022-07-06 12:50:00 【鄧嘉文Jarvan】
[算法] 劍指offer2 golang 面試題2:二進制加法
題目1:
給定兩個 01 字符串 a 和 b ,請計算它們的和,並以二進制字符串的形式輸出。
輸入為 非空 字符串且只包含數字 1 和 0。
示例 1:
輸入: a = “11”, b = “10”
輸出: “101”
示例 2:
輸入: a = “1010”, b = “1011”
輸出: “10101”
提示:
每個字符串僅由字符 ‘0’ 或 ‘1’ 組成。
1 <= a.length, b.length <= 10^4
字符串如果不是 “0” ,就都不含前導零。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/JFETK5
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
思路1: 模擬二進制加法
模擬二進制加法時間複雜度 O(n)
代碼
func addBinary(a string, b string) string {
//思路: 模擬二進制計算
length := len(a)
if len(b) > len(a) {
length = len(b)
}
//構造返回
resBs := make([]byte, length)
//從後往前指向的指針
aP, bP := len(a)-1, len(b)-1
//是否進比特
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--
}
// 統計個數
count1 := 0
if aByte == '1' {
count1++
}
if bByte == '1' {
count1++
}
count1 += up
//通過個數確定當前值和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'
}
}
//最後的up
res := string(resBs)
if up == 1 {
res = "1" + res
}
return res
}
測試

边栏推荐
- Combination of fairygui check box and progress bar
- 单片机蓝牙无线烧录
- InnoDB dirty page refresh mechanism checkpoint in MySQL
- [leetcode15] sum of three numbers
- [offer78]合并多个有序链表
- KF UD分解之UD分解基础篇【1】
- Theoretical derivation of support vector machine
- wsl常用命令
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
猜你喜欢

Unity3d, Alibaba cloud server, platform configuration

What are the advantages of using SQL in Excel VBA

(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图

Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY

Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
![Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]](/img/b0/176bf6dea2201afc892d6750c5974b.png)
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]

ORA-02030: can only select from fixed tables/views
![[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等](/img/11/ee0628a68542236fc641966579a31a.png)
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等

Office prompts that your license is not genuine pop-up box solution

341. Flatten nested list iterator
随机推荐
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
[leetcode622] design circular queue
Fairygui gain buff value change display
MySQL performance tuning - dirty page refresh
Esp8266 connect onenet (old mqtt mode)
What are the functions and features of helm or terrain
【RTKLIB 2.4.3 b34 】版本更新简介一
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
使用rtknavi进行RT-PPP测试
数据库课程设计:高校教务管理系统(含代码)
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Devops' future: six trends in 2022 and beyond
KF UD分解之UD分解基础篇【1】
MySQL error warning: a long semaphore wait
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
Single chip Bluetooth wireless burning
Unity scene jump and exit
单片机蓝牙无线烧录
【无标题】
C programming exercise