🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update: Add Binary
  • Loading branch information
openset committed Sep 18, 2019
commit 92cf8ea4c802b8aa27c285e6beeb5b32827fdcd9
24 changes: 11 additions & 13 deletions problems/add-binary/add_binary.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package add_binary
package problem_67

func addBinary(a string, b string) string {
i, j := len(a)-1, len(b)-1
var carry byte = '0'
var bs []byte
for i >= 0 || j >= 0 || carry != '0' {
ans, l1, l2, carry := "", len(a)-1, len(b)-1, byte('0')
for l1 >= 0 || l2 >= 0 || carry != '0' {
v := carry
if i >= 0 {
v += a[i] - '0'
i--
if l1 >= 0 {
v += a[l1] - '0'
l1--
}
if j >= 0 {
v += b[j] - '0'
j--
if l2 >= 0 {
v += b[l2] - '0'
l2--
}
carry = '0' + (v-'0')/2
v = '0' + (v-'0')%2
bs = append([]byte{v}, bs...)
ans = string(v) + ans
}
return string(bs)
return ans
}
2 changes: 1 addition & 1 deletion problems/add-binary/add_binary_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package add_binary
package problem_67

import "testing"

Expand Down