当前位置:网站首页>[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
2022-07-06 12:50:00 【Duncan jarvan】
[Algorithmes] Un doigt d'épée.offer2 golang Questions d'entrevue2:Addition binaire
Titre1:
Compte tenu de deux 01 String a Et b ,Veuillez calculer leur somme,Et sortie sous forme de chaîne binaire.
Entrée comme Non vide Chaîne et ne contient que des nombres 1 Et 0.
Exemple 1:
Entrée: a = “11”, b = “10”
Produits: “101”
Exemple 2:
Entrée: a = “1010”, b = “1011”
Produits: “10101”
Conseils:
Chaque chaîne est composée uniquement de caractères ‘0’ Ou ‘1’ Composition.
1 <= a.length, b.length <= 10^4
Chaîne sinon “0” ,Il n'y a pas de zéros d'avance..
Source::Boucle de force(LeetCode)
Liens:https://leetcode-cn.com/problems/JFETK5
Le droit d'auteur est la propriété du réseau de saisie.Pour les réimpressions commerciales, veuillez contacter l'autorisation officielle,Reproduction non commerciale Veuillez indiquer la source.
Idées1: Addition binaire analogique
Complexité temporelle de l'addition binaire analogique O(n)
Code
func addBinary(a string, b string) string {
//Idées: Calcul binaire analogique
length := len(a)
if len(b) > len(a) {
length = len(b)
}
//Retour de la construction
resBs := make([]byte, length)
// Pointeur vers l'arrière et vers l'avant
aP, bP := len(a)-1, len(b)-1
//Oui Non
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--
}
// Nombre de statistiques
count1 := 0
if aByte == '1' {
count1++
}
if bByte == '1' {
count1++
}
count1 += up
// Déterminer la valeur actuelle et 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'
}
}
//Le dernier.up
res := string(resBs)
if up == 1 {
res = "1" + res
}
return res
}
Tests

边栏推荐
- [Offer29] 排序的循环链表
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- 341. Flatten nested list iterator
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- [算法] 剑指offer2 golang 面试题10:和为k的子数组
- Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- 数据库课程设计:高校教务管理系统(含代码)
- 如何给Arduino项目添加音乐播放功能
- Mysql database index
猜你喜欢
![[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和

Liste des boucles de l'interface graphique de défaillance

idea中导包方法

First use of dosbox

Derivation of logistic regression theory

2021.11.10汇编考试

Gravure sans fil Bluetooth sur micro - ordinateur à puce unique

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

FairyGUI增益BUFF數值改變的顯示

Force buckle 1189 Maximum number of "balloons"
随机推荐
FairyGUI簡單背包的制作
341. Flatten nested list iterator
Lean product development - Lean Software Development & lean product development
Teach you to release a DeNO module hand in hand
KF UD分解之伪代码实现进阶篇【2】
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Servlet
平衡二叉树详解 通俗易懂
基本Dos命令
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
第一人称视角的角色移动
【RTKLIB 2.4.3 b34 】版本更新简介一
地球围绕太阳转
[899]有序队列
[leetcode19] delete the penultimate node in the linked list
2021.11.10汇编考试
Mysql database index
Vulnhub target: hacknos_ PLAYER V1.1
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data