Skip to content

Commit 8ac42a2

Browse files
committed
Add solution and test-cases for problem 3280
1 parent f03226b commit 8ac42a2

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [3280.Convert Date to Binary][title]
2+
3+
## Description
4+
You are given a string `date` representing a Gregorian calendar date in the `yyyy-mm-dd` format.
5+
6+
`date` can be written in its binary representation obtained by converting year, month, and day to their binary representations without any leading zeroes and writing them down in `year-month-day` format.
7+
8+
Return the **binary** representation of `date`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: date = "2080-02-29"
14+
15+
Output: "100000100000-10-11101"
16+
17+
Explanation:
18+
19+
100000100000, 10, and 11101 are the binary representations of 2080, 02, and 29 respectively.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: date = "1900-01-01"
26+
27+
Output: "11101101100-1-1"
28+
29+
Explanation:
30+
31+
11101101100, 1, and 1 are the binary representations of 1900, 1, and 1 respectively.
32+
```
33+
34+
## 结语
35+
36+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
37+
38+
[title]: https://leetcode.com/problems/convert-date-to-binary
39+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Solution
2+
3+
import "bytes"
4+
5+
func convert(s string) string {
6+
base := 0
7+
for _, b := range s {
8+
base = base*10 + int(b-'0')
9+
}
10+
buf := bytes.NewBuffer([]byte{})
11+
for base > 0 {
12+
mod := base & 1
13+
if mod == 1 {
14+
buf.WriteByte('1')
15+
} else {
16+
buf.WriteByte('0')
17+
}
18+
base >>= 1
19+
}
20+
bs := buf.Bytes()
21+
for s, e := 0, len(bs)-1; s < e; s, e = s+1, e-1 {
22+
bs[s], bs[e] = bs[e], bs[s]
23+
}
24+
return string(bs)
25+
}
26+
27+
func Solution(date string) string {
28+
// 0:4, 5:7 8:end
29+
return convert(date[:4]) + "-" + convert(date[5:7]) + "-" + convert(date[8:])
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs string
14+
expect string
15+
}{
16+
{"TestCase1", "2080-02-29", "100000100000-10-11101"},
17+
{"TestCase2", "1900-01-01", "11101101100-1-1"},
18+
}
19+
20+
// 开始测试
21+
for i, c := range cases {
22+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
23+
got := Solution(c.inputs)
24+
if !reflect.DeepEqual(got, c.expect) {
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26+
c.expect, got, c.inputs)
27+
}
28+
})
29+
}
30+
}
31+
32+
// 压力测试
33+
func BenchmarkSolution(b *testing.B) {
34+
}
35+
36+
// 使用案列
37+
func ExampleSolution() {
38+
}

0 commit comments

Comments
 (0)