-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
- Loading branch information
Showing
4 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,8 +68,9 @@ seo: | |
## 解題思路 | ||
|
||
## Big O | ||
時間複雜 : `` | ||
空間複雜 : `` | ||
|
||
* 時間複雜 : `` | ||
* 空間複雜 : `` | ||
|
||
## 來源 | ||
* LEETCODELINK | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
title: 128. Longest Consecutive Sequence | ||
subtitle: "https://leetcode.com/problems/longest-consecutive-sequence/description/" | ||
date: 2024-02-20T20:53:00+08:00 | ||
lastmod: 2024-02-20T20:53:00+08:00 | ||
draft: false | ||
author: "Kimi.Tsai" | ||
authorLink: "https://kimi0230.github.io/" | ||
description: "0128.Longest-Consecutive-Sequence" | ||
license: "" | ||
images: [] | ||
|
||
tags: [LeetCode, Go, Easy/Medium/Hard, LEETCODETITLE] | ||
categories: [LeetCode] | ||
|
||
featuredImage: "" | ||
featuredImagePreview: "" | ||
|
||
hiddenFromHomePage: false | ||
hiddenFromSearch: false | ||
twemoji: false | ||
lightgallery: true | ||
ruby: true | ||
fraction: true | ||
fontawesome: true | ||
linkToMarkdown: false | ||
rssFullText: false | ||
|
||
toc: | ||
enable: true | ||
auto: true | ||
code: | ||
copy: true | ||
maxShownLines: 200 | ||
math: | ||
enable: false | ||
# ... | ||
mapbox: | ||
# ... | ||
share: | ||
enable: true | ||
# ... | ||
comment: | ||
enable: true | ||
# ... | ||
library: | ||
css: | ||
# someCSS = "some.css" | ||
# located in "assets/" | ||
# Or | ||
# someCSS = "https://cdn.example.com/some.css" | ||
js: | ||
# someJS = "some.js" | ||
# located in "assets/" | ||
# Or | ||
# someJS = "https://cdn.example.com/some.js" | ||
seo: | ||
images: [] | ||
# ... | ||
--- | ||
# [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/description/) | ||
|
||
## 題目 | ||
|
||
## 題目大意 | ||
|
||
|
||
## 解題思路 | ||
|
||
## Big O | ||
時間複雜 : `O(n)` | ||
空間複雜 : `O(n)` | ||
|
||
## 來源 | ||
* https://leetcode.com/problems/longest-consecutive-sequence/description/ | ||
* https://leetcode.cn/problems/longest-consecutive-sequence/description/ | ||
|
||
## 解答 | ||
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0128.Longest-Consecutive-Sequence/main.go | ||
|
||
```go | ||
package longestconsecutivesequence | ||
|
||
// 時間複雜 O(), 空間複雜 O() | ||
func longestConsecutive(nums []int) int { | ||
m := make(map[int]struct{}, len(nums)) | ||
for _, num := range nums { | ||
m[num] = struct{}{} | ||
} | ||
result := 0 | ||
for v := range m { | ||
// 如果沒找到該數字的前一個數字, 則把該數字刀做連續序列的第一個數 | ||
if _, ok := m[v-1]; !ok { | ||
length := 1 | ||
for _, exit := m[v+length]; exit; _, exit = m[v+length] { | ||
length++ | ||
} | ||
if result < length { | ||
result = length | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
``` | ||
|
||
## Benchmark | ||
|
||
```sh | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package longestconsecutivesequence | ||
|
||
// 時間複雜 O(), 空間複雜 O() | ||
func longestConsecutive(nums []int) int { | ||
m := make(map[int]struct{}, len(nums)) | ||
for _, num := range nums { | ||
m[num] = struct{}{} | ||
} | ||
result := 0 | ||
for v := range m { | ||
// 如果沒找到該數字的前一個數字, 則把該數字刀做連續序列的第一個數 | ||
if _, ok := m[v-1]; !ok { | ||
length := 1 | ||
for _, exit := m[v+length]; exit; _, exit = m[v+length] { | ||
length++ | ||
} | ||
if result < length { | ||
result = length | ||
} | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package longestconsecutivesequence | ||
|
||
import "testing" | ||
|
||
var tests = []struct { | ||
arg1 []int | ||
want int | ||
}{ | ||
{ | ||
[]int{100, 4, 200, 1, 3, 2}, | ||
4, | ||
}, | ||
} | ||
|
||
func TestLongestConsecutive(t *testing.T) { | ||
for _, tt := range tests { | ||
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) { | ||
if got := longestConsecutive(tt.arg1); got != tt.want { | ||
t.Errorf("got = %v, want = %v", got, tt.want) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLongestConsecutive(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
longestConsecutive(tests[0].arg1) | ||
} | ||
} | ||
|
||
/* | ||
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=. | ||
*/ |