Skip to content

Commit a6093a1

Browse files
committed
feat: 0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
1 parent af49301 commit a6093a1

File tree

4 files changed

+172
-2
lines changed

4 files changed

+172
-2
lines changed

Leetcode/0000.xxxx/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ seo:
6868
## 解題思路
6969

7070
## Big O
71-
時間複雜 : ``
72-
空間複雜 : ``
71+
72+
* 時間複雜 : ``
73+
* 空間複雜 : ``
7374

7475
## 來源
7576
* LEETCODELINK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: 128. Longest Consecutive Sequence
3+
subtitle: "https://leetcode.com/problems/longest-consecutive-sequence/description/"
4+
date: 2024-02-20T20:53:00+08:00
5+
lastmod: 2024-02-20T20:53:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0128.Longest-Consecutive-Sequence"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Easy/Medium/Hard, LEETCODETITLE]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
67+
68+
## 解題思路
69+
70+
## Big O
71+
時間複雜 : `O(n)`
72+
空間複雜 : `O(n)`
73+
74+
## 來源
75+
* https://leetcode.com/problems/longest-consecutive-sequence/description/
76+
* https://leetcode.cn/problems/longest-consecutive-sequence/description/
77+
78+
## 解答
79+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0128.Longest-Consecutive-Sequence/main.go
80+
81+
```go
82+
package longestconsecutivesequence
83+
84+
// 時間複雜 O(), 空間複雜 O()
85+
func longestConsecutive(nums []int) int {
86+
m := make(map[int]struct{}, len(nums))
87+
for _, num := range nums {
88+
m[num] = struct{}{}
89+
}
90+
result := 0
91+
for v := range m {
92+
// 如果沒找到該數字的前一個數字, 則把該數字刀做連續序列的第一個數
93+
if _, ok := m[v-1]; !ok {
94+
length := 1
95+
for _, exit := m[v+length]; exit; _, exit = m[v+length] {
96+
length++
97+
}
98+
if result < length {
99+
result = length
100+
}
101+
}
102+
}
103+
return result
104+
}
105+
106+
```
107+
108+
## Benchmark
109+
110+
```sh
111+
112+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package longestconsecutivesequence
2+
3+
// 時間複雜 O(), 空間複雜 O()
4+
func longestConsecutive(nums []int) int {
5+
m := make(map[int]struct{}, len(nums))
6+
for _, num := range nums {
7+
m[num] = struct{}{}
8+
}
9+
result := 0
10+
for v := range m {
11+
// 如果沒找到該數字的前一個數字, 則把該數字刀做連續序列的第一個數
12+
if _, ok := m[v-1]; !ok {
13+
length := 1
14+
for _, exit := m[v+length]; exit; _, exit = m[v+length] {
15+
length++
16+
}
17+
if result < length {
18+
result = length
19+
}
20+
}
21+
}
22+
return result
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package longestconsecutivesequence
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 []int
7+
want int
8+
}{
9+
{
10+
[]int{100, 4, 200, 1, 3, 2},
11+
4,
12+
},
13+
}
14+
15+
func TestLongestConsecutive(t *testing.T) {
16+
for _, tt := range tests {
17+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
18+
if got := longestConsecutive(tt.arg1); got != tt.want {
19+
t.Errorf("got = %v, want = %v", got, tt.want)
20+
}
21+
}
22+
}
23+
24+
func BenchmarkLongestConsecutive(b *testing.B) {
25+
b.ResetTimer()
26+
for i := 0; i < b.N; i++ {
27+
longestConsecutive(tests[0].arg1)
28+
}
29+
}
30+
31+
/*
32+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
33+
34+
*/

0 commit comments

Comments
 (0)