# 67. 二进制求和

PRE_ZHY · · 2302 次点击 · · 开始浏览

``````func addBinary(a string, b string) string {
ai, err := strconv.ParseInt(a, 2, 64)
if err != nil {
fmt.Println(ai, err)
}
bi, err := strconv.ParseInt(b, 2, 64)
return fmt.Sprintf("%b", ai+bi)
}
``````

c := "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101"
d := "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"

``````func binadd(x, y, flag int) (sum, addflag int) {
sum = (x + y + flag) % 2
addflag = (x + y + flag) / 2
return
}
``````

``````func binStr2Slice(BinStr string) []int {
temp := make([]int, len(BinStr), len(BinStr))
for i := 0; i < len(BinStr); i++ {
if BinStr[i] == '0' {
temp[i] = 0
} else {
temp[i] = 1
}
}
return temp
}

func binSlice2str(b []int) string {
s := ""
for _, v := range b {
s = s + fmt.Sprint(v)
}
return s
}
``````

``````func addBinaryS(a string, b string) string {
if len(a) < len(b) {
a, b = b, a
}
maxL := len(a)
aint := binStr2Slice(a)
bint := binStr2Slice(b)

sum := make([]int, maxL, maxL)
for i := 1; i <= maxL; i++ {
if i <= len(b) {
} else {
}

}
sum = append([]int{1}, sum...)
}
return binSlice2str(sum)
}
``````

`PASS`

``````func addBinary(a string, b string) string {
var carry, sum int
i, j := len(a), len(b)
if i < j {
i, j = j, i
a, b = b, a
}

res := make([]byte, i+1)

for j > 0 {
j--
i--
sum = int(a[i]-'0') + int(b[j]-'0') + carry
carry = sum / 2
sum = sum % 2
res[i+1] = byte(sum + '0')
}

for i > 0 {
i--
sum = int(a[i]-'0') + carry
carry = sum / 2
sum = sum % 2
res[i+1] = byte(sum + '0')
}

res[0] = byte(carry + '0')

for i < len(res)-1 {
if res[i] == '0' {
i++
} else {
break
}
}
return string(res[i:])
}
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

``````func addBinary(a string, b string) string {
ai, err := strconv.ParseInt(a, 2, 64)
if err != nil {
fmt.Println(ai, err)
}
bi, err := strconv.ParseInt(b, 2, 64)
return fmt.Sprintf("%b", ai+bi)
}
``````

c := "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101"
d := "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"

``````func binadd(x, y, flag int) (sum, addflag int) {
sum = (x + y + flag) % 2
addflag = (x + y + flag) / 2
return
}
``````

``````func binStr2Slice(BinStr string) []int {
temp := make([]int, len(BinStr), len(BinStr))
for i := 0; i < len(BinStr); i++ {
if BinStr[i] == '0' {
temp[i] = 0
} else {
temp[i] = 1
}
}
return temp
}

func binSlice2str(b []int) string {
s := ""
for _, v := range b {
s = s + fmt.Sprint(v)
}
return s
}
``````

``````func addBinaryS(a string, b string) string {
if len(a) < len(b) {
a, b = b, a
}
maxL := len(a)
aint := binStr2Slice(a)
bint := binStr2Slice(b)

sum := make([]int, maxL, maxL)
for i := 1; i <= maxL; i++ {
if i <= len(b) {
} else {
}

}
sum = append([]int{1}, sum...)
}
return binSlice2str(sum)
}
``````

`PASS`

``````func addBinary(a string, b string) string {
var carry, sum int
i, j := len(a), len(b)
if i < j {
i, j = j, i
a, b = b, a
}

res := make([]byte, i+1)

for j > 0 {
j--
i--
sum = int(a[i]-'0') + int(b[j]-'0') + carry
carry = sum / 2
sum = sum % 2
res[i+1] = byte(sum + '0')
}

for i > 0 {
i--
sum = int(a[i]-'0') + carry
carry = sum / 2
sum = sum % 2
res[i+1] = byte(sum + '0')
}

res[0] = byte(carry + '0')

for i < len(res)-1 {
if res[i] == '0' {
i++
} else {
break
}
}
return string(res[i:])
}
``````