当前位置:网站首页>[算法] 剑指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
}
测试

边栏推荐
- Solution to the problem of automatic login in Yanshan University Campus Network
- Programming homework: educational administration management system (C language)
- 微信小程序开发心得
- Unity3d makes the registration login interface and realizes the scene jump
- Vulnhub target: hacknos_ PLAYER V1.1
- Force buckle 1189 Maximum number of "balloons"
- 程序设计大作业:教务管理系统(C语言)
- Mysql database index
- Idea problem record
- Introduction to the daily practice column of the Blue Bridge Cup
猜你喜欢

ORA-02030: can only select from fixed tables/views

Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance

First use of dosbox

Fabrication d'un sac à dos simple fairygui

(core focus of software engineering review) Chapter V detailed design exercises

Fairygui gain buff value change display

Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports

Unity场景跳转及退出

数据库课程设计:高校教务管理系统(含代码)

Office提示您的许可证不是正版弹框解决
随机推荐
rtklib单点定位spp使用抗差估计遇到的问题及解决
Devops' future: six trends in 2022 and beyond
Fairygui joystick
[leetcode19] delete the penultimate node in the linked list
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
KF UD分解之伪代码实现进阶篇【2】
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
Excel导入,导出功能实现
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
Expected value (EV)
MySQL replacement field part content
Guided package method in idea
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
FairyGUI循環列錶
C programming exercise
FairyGUI摇杆
【GNSS】抗差估计(稳健估计)原理及程序实现
MySQL performance tuning - dirty page refresh
idea中好用的快捷键
ORA-02030: can only select from fixed tables/views