mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
添加 18 题
This commit is contained in:
38
leetcode/0067.Add-Binary/67. Add Binary.go
Normal file
38
leetcode/0067.Add-Binary/67. Add Binary.go
Normal file
@ -0,0 +1,38 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addBinary(a string, b string) string {
|
||||
if len(b) > len(a) {
|
||||
a, b = b, a
|
||||
}
|
||||
|
||||
res := make([]string, len(a)+1)
|
||||
i, j, k, c := len(a)-1, len(b)-1, len(a), 0
|
||||
for i >= 0 && j >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
bj, _ := strconv.Atoi(string(b[j]))
|
||||
res[k] = strconv.Itoa((ai + bj + c) % 2)
|
||||
c = (ai + bj + c) / 2
|
||||
i--
|
||||
j--
|
||||
k--
|
||||
}
|
||||
|
||||
for i >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
res[k] = strconv.Itoa((ai + c) % 2)
|
||||
c = (ai + c) / 2
|
||||
i--
|
||||
k--
|
||||
}
|
||||
|
||||
if c > 0 {
|
||||
res[k] = strconv.Itoa(c)
|
||||
}
|
||||
|
||||
return strings.Join(res, "")
|
||||
}
|
48
leetcode/0067.Add-Binary/67. Add Binary_test.go
Normal file
48
leetcode/0067.Add-Binary/67. Add Binary_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question67 struct {
|
||||
para67
|
||||
ans67
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para67 struct {
|
||||
a string
|
||||
b string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans67 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem67(t *testing.T) {
|
||||
|
||||
qs := []question67{
|
||||
|
||||
question67{
|
||||
para67{"11", "1"},
|
||||
ans67{"100"},
|
||||
},
|
||||
|
||||
question67{
|
||||
para67{"1010", "1011"},
|
||||
ans67{"10101"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 67------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans67, q.para67
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, addBinary(p.a, p.b))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
76
leetcode/0067.Add-Binary/README.md
Normal file
76
leetcode/0067.Add-Binary/README.md
Normal file
@ -0,0 +1,76 @@
|
||||
# [67. Add Binary](https://leetcode.com/problems/add-binary/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given two binary strings, return their sum (also a binary string).
|
||||
|
||||
The input strings are both **non-empty** and contains only characters `1` or `0`.
|
||||
|
||||
**Example 1**:
|
||||
|
||||
```
|
||||
Input: a = "11", b = "1"
|
||||
Output: "100"
|
||||
```
|
||||
|
||||
**Example 2**:
|
||||
|
||||
```
|
||||
Input: a = "1010", b = "1011"
|
||||
Output: "10101"
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你两个二进制字符串,返回它们的和(用二进制表示)。输入为 非空 字符串且只包含数字 1 和 0。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 要求输出 2 个二进制数的和,结果也用二进制表示。
|
||||
- 简单题。按照二进制的加法规则做加法即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addBinary(a string, b string) string {
|
||||
if len(b) > len(a) {
|
||||
a, b = b, a
|
||||
}
|
||||
|
||||
res := make([]string, len(a)+1)
|
||||
i, j, k, c := len(a)-1, len(b)-1, len(a), 0
|
||||
for i >= 0 && j >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
bj, _ := strconv.Atoi(string(b[j]))
|
||||
res[k] = strconv.Itoa((ai + bj + c) % 2)
|
||||
c = (ai + bj + c) / 2
|
||||
i--
|
||||
j--
|
||||
k--
|
||||
}
|
||||
|
||||
for i >= 0 {
|
||||
ai, _ := strconv.Atoi(string(a[i]))
|
||||
res[k] = strconv.Itoa((ai + c) % 2)
|
||||
c = (ai + c) / 2
|
||||
i--
|
||||
k--
|
||||
}
|
||||
|
||||
if c > 0 {
|
||||
res[k] = strconv.Itoa(c)
|
||||
}
|
||||
|
||||
return strings.Join(res, "")
|
||||
}
|
||||
|
||||
```
|
Reference in New Issue
Block a user