当前位置:网站首页>[算法] 剑指offer2 golang 面试题2:二进制加法
[算法] 剑指offer2 golang 面试题2:二进制加法
2022-07-06 09:18: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
}
测试

边栏推荐
- Lean product development - Lean Software Development & lean product development
- Fairygui character status Popup
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- Minio file download problem - inputstream:closed
- (core focus of software engineering review) Chapter V detailed design exercises
- Unity3D制作注册登录界面,并实现场景跳转
- Office prompts that your license is not genuine pop-up box solution
- HCIP Day 12
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
- InnoDB dirty page refresh mechanism checkpoint in MySQL
猜你喜欢
![[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data](/img/28/221b0a51ef5f2e8ed5aeca2de8f463.jpg)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data

2021.11.10汇编考试

Fairygui loop list

Office提示您的许可证不是正版弹框解决

Vulnhub target: hacknos_ PLAYER V1.1

341. Flatten nested list iterator

Mysql database index

地球围绕太阳转

Single chip Bluetooth wireless burning

FGUI工程打包发布&导入Unity&将UI显示出来的方式
随机推荐
Database course design: college educational administration management system (including code)
idea中好用的快捷键
NRF24L01故障排查
Excel导入,导出功能实现
Unity3D制作注册登录界面,并实现场景跳转
FairyGUI增益BUFF数值改变的显示
wsl常用命令
Fairygui gain buff value change display
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
[leetcode19] delete the penultimate node in the linked list
In 2020, the average salary of IT industry exceeded 170000, ranking first
341. Flatten nested list iterator
音乐播放(Toggle && PlayerPrefs)
平衡二叉树详解 通俗易懂
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
如何给Arduino项目添加音乐播放功能
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Minio file download problem - inputstream:closed
Fairygui loop list