Skip to content

Commit 417be87

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents b72b89d + d1e2c8e commit 417be87

File tree

9 files changed

+486
-0
lines changed

9 files changed

+486
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package Soluation
2+
3+
var p map[int]int
4+
var rank map[int]int
5+
6+
func longestConsecutive(nums []int) int {
7+
l := len(nums)
8+
if l < 1 {
9+
return 0
10+
}
11+
p, rank = initialize(nums)
12+
hash := make(map[int]int)
13+
for _, v := range nums {
14+
hash[v] = 1
15+
if _, ok := hash[v-1]; ok {
16+
union(v, v-1)
17+
}
18+
if _, ok := hash[v+1]; ok {
19+
union(v, v+1)
20+
}
21+
}
22+
23+
ans := 0
24+
for _, v := range rank {
25+
if v > ans {
26+
ans = v
27+
}
28+
}
29+
return ans
30+
}
31+
32+
func initialize(nums []int) (map[int]int, map[int]int) {
33+
p, rank = make(map[int]int), make(map[int]int)
34+
for _, v := range nums {
35+
p[v] = v
36+
rank[v] = 1
37+
}
38+
return p, rank
39+
}
40+
41+
func find(x int) int {
42+
if p[x] != x {
43+
p[x] = find(p[x])
44+
}
45+
return p[x]
46+
}
47+
48+
func union(x, y int) {
49+
x = find(x)
50+
y = find(y)
51+
if x != y {
52+
if rank[x] >= rank[y] {
53+
rank[x] += rank[y]
54+
p[y] = x
55+
} else {
56+
rank[y] += rank[x]
57+
p[x] = y
58+
}
59+
}
60+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Soluation
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSolution(t *testing.T) {
9+
// 测试用例
10+
cases := []struct {
11+
name string
12+
inputs []int
13+
expect int
14+
}{
15+
{
16+
"TestCases 1",
17+
[]int{100, 4, 200, 1, 3, 2},
18+
4,
19+
},
20+
}
21+
22+
// 开始测试
23+
for _, c := range cases {
24+
t.Run(c.name, func(t *testing.T) {
25+
got := longestConsecutive(c.inputs)
26+
if !reflect.DeepEqual(got, c.expect) {
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
36+
}
37+
38+
// 使用案列
39+
func ExampleSolution() {
40+
41+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# [128. Longest Consecutive Sequence][title]
2+
3+
## Description
4+
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
5+
6+
Your algorithm should run in O(n) complexity.
7+
8+
**Example:**
9+
10+
```
11+
Input: [100, 4, 200, 1, 3, 2]
12+
Output: 4
13+
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
14+
```
15+
16+
## 题意
17+
> 给定一个未排序的整数数组,找出最长连续序列的长度。
18+
19+
20+
## 题解
21+
22+
### 思路1
23+
> 使用并查集,将相邻的元素合并,找到元素最多的集合。
24+
25+
```go
26+
var p map[int]int
27+
var rank map[int]int
28+
29+
func longestConsecutive(nums []int) int {
30+
l := len(nums)
31+
if l < 1 {
32+
return 0
33+
}
34+
p, rank = initialize(nums)
35+
hash := make(map[int]int)
36+
for _, v := range nums {
37+
hash[v] = 1
38+
if _, ok := hash[v-1]; ok {
39+
union(v, v-1)
40+
}
41+
if _, ok := hash[v+1]; ok {
42+
union(v, v+1)
43+
}
44+
}
45+
46+
ans := 0
47+
for _, v := range rank {
48+
if v > ans {
49+
ans = v
50+
}
51+
}
52+
fmt.Println(rank)
53+
return ans
54+
}
55+
56+
func initialize(nums []int) (map[int]int, map[int]int) {
57+
p, rank = make(map[int]int), make(map[int]int)
58+
for _, v := range nums {
59+
p[v] = v
60+
rank[v] = 1
61+
}
62+
return p, rank
63+
}
64+
65+
func find(x int) int {
66+
if p[x] != x {
67+
p[x] = find(p[x])
68+
}
69+
return p[x]
70+
}
71+
72+
func union(x, y int) {
73+
x = find(x)
74+
y = find(y)
75+
if x != y {
76+
if rank[x] >= rank[y] {
77+
rank[x] += rank[y]
78+
p[y] = x
79+
} else {
80+
rank[y] += rank[x]
81+
p[x] = y
82+
}
83+
}
84+
}
85+
```
86+
87+
### 思路2
88+
> 思路2
89+
```go
90+
91+
```
92+
93+
## 结语
94+
95+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
96+
97+
[title]: https://leetcode.com/problems/longest-consecutive-sequence/description/
98+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Soluation
2+
3+
func findLengthOfLCIS(nums []int) int {
4+
if 0 == len(nums) {
5+
return 0
6+
}
7+
ans, sum := 1, 1
8+
for i := 1; i < len(nums); i++ {
9+
if nums[i] > nums[i-1] {
10+
sum++
11+
} else {
12+
sum = 1
13+
}
14+
if sum > ans {
15+
ans = sum
16+
}
17+
}
18+
return ans
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package Soluation
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSolution(t *testing.T) {
9+
// 测试用例
10+
cases := []struct {
11+
name string
12+
inputs []int
13+
expect int
14+
}{
15+
{
16+
"TestCases 1",
17+
[]int{1,3,5,4,7},
18+
3,
19+
},
20+
{
21+
"TestCases 2",
22+
[]int{2,2,2,2,2},
23+
1,
24+
},
25+
}
26+
27+
// 开始测试
28+
for _, c := range cases {
29+
t.Run(c.name, func(t *testing.T) {
30+
got := findLengthOfLCIS(c.inputs)
31+
if !reflect.DeepEqual(got, c.expect) {
32+
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
33+
}
34+
})
35+
}
36+
}
37+
38+
// 压力测试
39+
func BenchmarkSolution(b *testing.B) {
40+
41+
}
42+
43+
// 使用案列
44+
func ExampleSolution() {
45+
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# [674. Longest Continuous Increasing Subsequence][title]
2+
3+
## Description
4+
5+
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
6+
7+
**Example 1:**
8+
```
9+
Input: [1,3,5,4,7]
10+
Output: 3
11+
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
12+
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
13+
```
14+
15+
**Example 2:**
16+
```
17+
Input: [2,2,2,2,2]
18+
Output: 1
19+
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
20+
```
21+
22+
## Note
23+
Length of the array will not exceed 10,000.
24+
25+
## 题意
26+
> 求一个数组的最长连续上升序列长度。
27+
28+
## 题解
29+
30+
### 思路 1
31+
> 判断相邻两个元素,后一个是否大于前一个,如果是就 +1,否则重置计数器。
32+
33+
```go
34+
func findLengthOfLCIS(nums []int) int {
35+
if 0 == len(nums) {
36+
return 0
37+
}
38+
ans, sum := 1, 1
39+
for i := 1; i < len(nums); i++ {
40+
if nums[i] > nums[i-1] {
41+
sum++
42+
} else {
43+
sum = 1
44+
}
45+
if sum > ans {
46+
ans = sum
47+
}
48+
}
49+
return ans
50+
}
51+
```
52+
53+
## 结语
54+
55+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
56+
57+
[title]: https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/
58+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Soluation
2+
3+
func maxAreaOfIsland(grid [][]int) int {
4+
ans := 0
5+
height, width := len(grid), len(grid[0])
6+
7+
for i := 0; i < height; i++ {
8+
for j := 0; j < width; j++ {
9+
tmp := 0;
10+
if 1 == grid[i][j] {
11+
tmp = dfs(grid, i, j)
12+
if tmp > ans {
13+
ans = tmp
14+
}
15+
}
16+
}
17+
}
18+
return ans
19+
}
20+
21+
func dfs(grid [][]int, i, j int) int {
22+
res := 0
23+
if i >= 0 && i < len(grid) && j >= 0 && j < len(grid[0]) && 1 == grid[i][j] {
24+
grid[i][j] = 0
25+
up := dfs(grid, i-1, j)
26+
down := dfs(grid, i+1, j)
27+
left := dfs(grid, i, j-1)
28+
right := dfs(grid, i, j+1)
29+
res = up + down + left + right + 1
30+
}
31+
return res
32+
}

0 commit comments

Comments
 (0)