当前位置:网站首页>[算法] 剑指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
}
测试
边栏推荐
- There is no red exclamation mark after SVN update
- 燕山大学校园网自动登录问题解决方案
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
- Office提示您的许可证不是正版弹框解决
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- 如何给Arduino项目添加音乐播放功能
- Talking about the startup of Oracle Database
- Devops' future: six trends in 2022 and beyond
- FairyGUI增益BUFF數值改變的顯示
猜你喜欢
What are the advantages of using SQL in Excel VBA
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
Unity3D制作注册登录界面,并实现场景跳转
FairyGUI人物状态弹窗
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
音乐播放(Toggle && PlayerPrefs)
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
FairyGUI复选框与进度条的组合使用
【干货】提升RTK模糊度固定率的建议之周跳探测
随机推荐
Unity3D,阿里云服务器,平台配置
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
Unity3d, Alibaba cloud server, platform configuration
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Lean product development - Lean Software Development & lean product development
[offer78]合并多个有序链表
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Derivation of logistic regression theory
FairyGUI按钮动效的混用
Mysql database index
Idea problem record
MySQL replacement field part content
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
Office prompts that your license is not genuine pop-up box solution
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
基于rtklib源码进行片上移植的思路分享
Conditional probability
C programming exercise
Gateway fails to route according to the service name, and reports an error service unavailable, status=503