u
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/number-of-islands/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
func numIslands(grid [][]byte) int {
|
||||||
|
m := len(grid)
|
||||||
|
n := len(grid[0])
|
||||||
|
visited := make([][]bool, m)
|
||||||
|
for i := range m {
|
||||||
|
visited[i] = make([]bool, n)
|
||||||
|
}
|
||||||
|
res := 0
|
||||||
|
type point struct {
|
||||||
|
Row int
|
||||||
|
Column int
|
||||||
|
}
|
||||||
|
queue := []point{}
|
||||||
|
for i := range m {
|
||||||
|
for j := range n {
|
||||||
|
if !visited[i][j] && grid[i][j] == '1' {
|
||||||
|
queue = append(queue, point{i, j})
|
||||||
|
visited[i][j] = true
|
||||||
|
res++
|
||||||
|
}
|
||||||
|
for len(queue) > 0 {
|
||||||
|
cur := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
// 左侧
|
||||||
|
if cur.Column > 0 && !visited[cur.Row][cur.Column-1] && grid[cur.Row][cur.Column-1] == '1' {
|
||||||
|
queue = append(queue, point{cur.Row, cur.Column - 1})
|
||||||
|
visited[cur.Row][cur.Column-1] = true
|
||||||
|
}
|
||||||
|
// 右侧
|
||||||
|
if cur.Column+1 < n && !visited[cur.Row][cur.Column+1] && grid[cur.Row][cur.Column+1] == '1' {
|
||||||
|
queue = append(queue, point{cur.Row, cur.Column + 1})
|
||||||
|
visited[cur.Row][cur.Column+1] = true
|
||||||
|
}
|
||||||
|
// 上侧
|
||||||
|
if cur.Row > 0 && !visited[cur.Row-1][cur.Column] && grid[cur.Row-1][cur.Column] == '1' {
|
||||||
|
queue = append(queue, point{cur.Row - 1, cur.Column})
|
||||||
|
visited[cur.Row-1][cur.Column] = true
|
||||||
|
}
|
||||||
|
// 下侧
|
||||||
|
if cur.Row+1 < m && !visited[cur.Row+1][cur.Column] && grid[cur.Row+1][cur.Column] == '1' {
|
||||||
|
queue = append(queue, point{cur.Row + 1, cur.Column})
|
||||||
|
visited[cur.Row+1][cur.Column] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test(t *testing.T) {
|
||||||
|
input := [][]byte{{'1', '1', '1'}, {'0', '1', '0'}, {'1', '1', '1'}}
|
||||||
|
fmt.Println(numIslands(input))
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/rotting-oranges/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
type point struct {
|
||||||
|
Row int
|
||||||
|
Col int
|
||||||
|
}
|
||||||
|
|
||||||
|
// AC,成绩不错,但是写的比较复杂
|
||||||
|
// func getFreshOrangesAround(grid [][]int, p point) []point {
|
||||||
|
// m, n := len(grid), len(grid[0])
|
||||||
|
// r, c := p.Row, p.Col
|
||||||
|
// res := []point{}
|
||||||
|
// if r > 0 && grid[r-1][c] == 1 {
|
||||||
|
// res = append(res, point{r - 1, c})
|
||||||
|
// }
|
||||||
|
// if r < m-1 && grid[r+1][c] == 1 {
|
||||||
|
// res = append(res, point{r + 1, c})
|
||||||
|
// }
|
||||||
|
// if c > 0 && grid[r][c-1] == 1 {
|
||||||
|
// res = append(res, point{r, c - 1})
|
||||||
|
// }
|
||||||
|
// if c < n-1 && grid[r][c+1] == 1 {
|
||||||
|
// res = append(res, point{r, c + 1})
|
||||||
|
// }
|
||||||
|
// return res
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func orangesRotting(grid [][]int) int {
|
||||||
|
// m := len(grid)
|
||||||
|
// n := len(grid[0])
|
||||||
|
|
||||||
|
// queue := []point{}
|
||||||
|
// minutes := 0
|
||||||
|
// for row := range m {
|
||||||
|
// for col := range n {
|
||||||
|
// if grid[row][col] == 2 {
|
||||||
|
// queue = append(queue, point{row, col})
|
||||||
|
// minutes = -1
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for len(queue) > 0 {
|
||||||
|
// batch := []point{}
|
||||||
|
// for len(queue) > 0 {
|
||||||
|
// cur := queue[0]
|
||||||
|
// queue = queue[1:]
|
||||||
|
// freshOranges := getFreshOrangesAround(grid, cur)
|
||||||
|
// for _, p := range freshOranges {
|
||||||
|
// grid[p.Row][p.Col] = 2
|
||||||
|
// }
|
||||||
|
// batch = append(batch, freshOranges...)
|
||||||
|
// }
|
||||||
|
// minutes++
|
||||||
|
// queue = batch
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for row := range m {
|
||||||
|
// for col := range n {
|
||||||
|
// if grid[row][col] == 1 {
|
||||||
|
// return -1
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return minutes
|
||||||
|
// }
|
||||||
|
|
||||||
|
func orangesRotting(grid [][]int) int {
|
||||||
|
m, n := len(grid), len(grid[0])
|
||||||
|
queue := []point{}
|
||||||
|
fresh := 0
|
||||||
|
for r := range m {
|
||||||
|
for c := range n {
|
||||||
|
switch grid[r][c] {
|
||||||
|
case 2:
|
||||||
|
queue = append(queue, point{r, c})
|
||||||
|
case 1:
|
||||||
|
fresh++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if fresh == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
minutes := -1
|
||||||
|
dirs := []point{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
|
||||||
|
for len(queue) > 0 {
|
||||||
|
size := len(queue)
|
||||||
|
for range size {
|
||||||
|
cur := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
for _, d := range dirs {
|
||||||
|
nr, nc := cur.Row+d.Row, cur.Col+d.Col
|
||||||
|
if nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] == 1 {
|
||||||
|
grid[nr][nc] = 2
|
||||||
|
fresh--
|
||||||
|
queue = append(queue, point{nr, nc})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
minutes++
|
||||||
|
}
|
||||||
|
if fresh > 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return minutes
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test1(t *testing.T) {
|
||||||
|
input := [][]int{{2, 1, 1}, {1, 1, 0}, {0, 1, 1}}
|
||||||
|
fmt.Println(orangesRotting(input))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test2(t *testing.T) {
|
||||||
|
input := [][]int{{2, 1, 1}, {0, 1, 1}, {1, 0, 1}}
|
||||||
|
fmt.Println(orangesRotting(input))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test3(t *testing.T) {
|
||||||
|
input := [][]int{{0, 2}}
|
||||||
|
fmt.Println(orangesRotting(input))
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/course-schedule/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
|
func canFinish(numCourses int, prerequisites [][]int) bool {
|
||||||
|
adjacencyList := make([][]int, numCourses)
|
||||||
|
for _, prerequisit := range prerequisites {
|
||||||
|
from, to := prerequisit[1], prerequisit[0]
|
||||||
|
adjacencyList[from] = append(adjacencyList[from], to)
|
||||||
|
}
|
||||||
|
|
||||||
|
state := make([]int, numCourses)
|
||||||
|
|
||||||
|
var hasCycle func(int) bool
|
||||||
|
hasCycle = func(node int) bool {
|
||||||
|
if state[node] == 1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if state[node] == 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
state[node] = 1
|
||||||
|
if slices.ContainsFunc(adjacencyList[node], hasCycle) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
state[node] = 2
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < numCourses; i++ {
|
||||||
|
if hasCycle(i) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/implement-trie-prefix-tree/?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
children [26]*Node
|
||||||
|
isEnd bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Trie struct {
|
||||||
|
root *Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func Constructor() Trie {
|
||||||
|
return Trie{root: &Node{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Trie) Insert(word string) {
|
||||||
|
cur := this.root
|
||||||
|
for i := 0; i < len(word); i++ {
|
||||||
|
idx := word[i] - 'a'
|
||||||
|
if cur.children[idx] == nil {
|
||||||
|
cur.children[idx] = &Node{}
|
||||||
|
}
|
||||||
|
cur = cur.children[idx]
|
||||||
|
}
|
||||||
|
cur.isEnd = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Trie) Search(word string) bool {
|
||||||
|
cur := this.root
|
||||||
|
for i := 0; i < len(word); i++ {
|
||||||
|
idx := word[i] - 'a'
|
||||||
|
if cur.children[idx] == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
cur = cur.children[idx]
|
||||||
|
}
|
||||||
|
return cur.isEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Trie) StartsWith(prefix string) bool {
|
||||||
|
cur := this.root
|
||||||
|
for i := 0; i < len(prefix); i++ {
|
||||||
|
idx := prefix[i] - 'a'
|
||||||
|
if cur.children[idx] == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
cur = cur.children[idx]
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test(t *testing.T) {
|
||||||
|
trie := Constructor()
|
||||||
|
trie.Insert("apple")
|
||||||
|
fmt.Println(trie.Search("apple")) // 返回 True
|
||||||
|
fmt.Println(trie.Search("app")) // 返回 False
|
||||||
|
fmt.Println(trie.StartsWith("app")) // 返回 True
|
||||||
|
trie.Insert("app")
|
||||||
|
fmt.Println(trie.Search("app")) // 返回 True
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package top100liked
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://leetcode.cn/problems/permutations?envType=study-plan-v2&envId=top-100-liked
|
||||||
|
|
||||||
|
func permute(nums []int) [][]int {
|
||||||
|
l := len(nums)
|
||||||
|
fact := 1
|
||||||
|
for i := 2; i <= l; i++ {
|
||||||
|
fact *= i
|
||||||
|
}
|
||||||
|
res := make([][]int, 0, fact)
|
||||||
|
var helper func(nums []int, used []bool, r []int)
|
||||||
|
helper = func(nums []int, used []bool, r []int) {
|
||||||
|
for i, v := range used {
|
||||||
|
if !v {
|
||||||
|
used[i] = true
|
||||||
|
r = append(r, nums[i])
|
||||||
|
if len(r) == l {
|
||||||
|
cp := make([]int, l)
|
||||||
|
copy(cp, r)
|
||||||
|
res = append(res, cp)
|
||||||
|
} else {
|
||||||
|
helper(nums, used, r)
|
||||||
|
}
|
||||||
|
r = r[:len(r)-1]
|
||||||
|
used[i] = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
helper(nums, make([]bool, l), make([]int, 0))
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test(t *testing.T) {
|
||||||
|
fmt.Printf("%+v", permute([]int{5, 4, 6, 2}))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user