From ec07a9322b08f447eb272d58ffa12a77c70d0189 Mon Sep 17 00:00:00 2001 From: nite Date: Sat, 13 Jun 2026 03:04:54 +1000 Subject: [PATCH] u --- top-100-liked/{ => 1}/1_test.go | 0 top-100-liked/{ => 10}/10_1_test.go | 0 top-100-liked/{ => 10}/10_test.go | 0 top-100-liked/{ => 11}/11_test.go | 0 top-100-liked/{ => 12}/12_test.go | 0 top-100-liked/{ => 13}/13_1_test.go | 0 top-100-liked/{ => 13}/13_test.go | 0 top-100-liked/{ => 14}/14_test.go | 0 top-100-liked/{ => 15}/15_test.go | 0 top-100-liked/{ => 16}/16_test.go | 0 top-100-liked/{ => 17}/17_test.go | 0 top-100-liked/18/18_test.go | 50 +++++++++++++++++ top-100-liked/19/19_test.go | 84 +++++++++++++++++++++++++++++ top-100-liked/{ => 2}/2_test.go | 0 top-100-liked/20/20_test.go | 30 +++++++++++ top-100-liked/21/21_test.go | 44 +++++++++++++++ top-100-liked/{ => 3}/3_test.go | 0 top-100-liked/{ => 4}/4_test.go | 0 top-100-liked/{ => 5}/5_test.go | 0 top-100-liked/{ => 6}/6_test.go | 0 top-100-liked/{ => 7}/7_test.go | 0 top-100-liked/{ => 8}/8_test.go | 0 top-100-liked/{ => 9}/9_test.go | 0 23 files changed, 208 insertions(+) rename top-100-liked/{ => 1}/1_test.go (100%) rename top-100-liked/{ => 10}/10_1_test.go (100%) rename top-100-liked/{ => 10}/10_test.go (100%) rename top-100-liked/{ => 11}/11_test.go (100%) rename top-100-liked/{ => 12}/12_test.go (100%) rename top-100-liked/{ => 13}/13_1_test.go (100%) rename top-100-liked/{ => 13}/13_test.go (100%) rename top-100-liked/{ => 14}/14_test.go (100%) rename top-100-liked/{ => 15}/15_test.go (100%) rename top-100-liked/{ => 16}/16_test.go (100%) rename top-100-liked/{ => 17}/17_test.go (100%) create mode 100644 top-100-liked/18/18_test.go create mode 100644 top-100-liked/19/19_test.go rename top-100-liked/{ => 2}/2_test.go (100%) create mode 100644 top-100-liked/20/20_test.go create mode 100644 top-100-liked/21/21_test.go rename top-100-liked/{ => 3}/3_test.go (100%) rename top-100-liked/{ => 4}/4_test.go (100%) rename top-100-liked/{ => 5}/5_test.go (100%) rename top-100-liked/{ => 6}/6_test.go (100%) rename top-100-liked/{ => 7}/7_test.go (100%) rename top-100-liked/{ => 8}/8_test.go (100%) rename top-100-liked/{ => 9}/9_test.go (100%) diff --git a/top-100-liked/1_test.go b/top-100-liked/1/1_test.go similarity index 100% rename from top-100-liked/1_test.go rename to top-100-liked/1/1_test.go diff --git a/top-100-liked/10_1_test.go b/top-100-liked/10/10_1_test.go similarity index 100% rename from top-100-liked/10_1_test.go rename to top-100-liked/10/10_1_test.go diff --git a/top-100-liked/10_test.go b/top-100-liked/10/10_test.go similarity index 100% rename from top-100-liked/10_test.go rename to top-100-liked/10/10_test.go diff --git a/top-100-liked/11_test.go b/top-100-liked/11/11_test.go similarity index 100% rename from top-100-liked/11_test.go rename to top-100-liked/11/11_test.go diff --git a/top-100-liked/12_test.go b/top-100-liked/12/12_test.go similarity index 100% rename from top-100-liked/12_test.go rename to top-100-liked/12/12_test.go diff --git a/top-100-liked/13_1_test.go b/top-100-liked/13/13_1_test.go similarity index 100% rename from top-100-liked/13_1_test.go rename to top-100-liked/13/13_1_test.go diff --git a/top-100-liked/13_test.go b/top-100-liked/13/13_test.go similarity index 100% rename from top-100-liked/13_test.go rename to top-100-liked/13/13_test.go diff --git a/top-100-liked/14_test.go b/top-100-liked/14/14_test.go similarity index 100% rename from top-100-liked/14_test.go rename to top-100-liked/14/14_test.go diff --git a/top-100-liked/15_test.go b/top-100-liked/15/15_test.go similarity index 100% rename from top-100-liked/15_test.go rename to top-100-liked/15/15_test.go diff --git a/top-100-liked/16_test.go b/top-100-liked/16/16_test.go similarity index 100% rename from top-100-liked/16_test.go rename to top-100-liked/16/16_test.go diff --git a/top-100-liked/17_test.go b/top-100-liked/17/17_test.go similarity index 100% rename from top-100-liked/17_test.go rename to top-100-liked/17/17_test.go diff --git a/top-100-liked/18/18_test.go b/top-100-liked/18/18_test.go new file mode 100644 index 0000000..b8ae1b3 --- /dev/null +++ b/top-100-liked/18/18_test.go @@ -0,0 +1,50 @@ +package top100liked + +// https://leetcode.cn/problems/set-matrix-zeroes/description/?envType=study-plan-v2&envId=top-100-liked + +import ( + "fmt" + "testing" +) + +func setZeroes(matrix [][]int) { + rowLength := len(matrix) + columeLength := len(matrix[0]) + + rowsHaveZero := make([]bool, rowLength) + columesHaveZero := make([]bool, columeLength) + + for rowIdx, row := range matrix { + for columeIdx := range row { + if matrix[rowIdx][columeIdx] == 0 { + if !columesHaveZero[columeIdx] { + for i := range rowIdx { + matrix[i][columeIdx] = 0 + } + } + if !rowsHaveZero[rowIdx] { + for i := range columeIdx { + matrix[rowIdx][i] = 0 + } + } + rowsHaveZero[rowIdx] = true + columesHaveZero[columeIdx] = true + } + if rowsHaveZero[rowIdx] || columesHaveZero[columeIdx] { + matrix[rowIdx][columeIdx] = 0 + } + } + } +} + +func TestS18(t *testing.T) { + var matrix [][]int + + matrix = [][]int{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}} + setZeroes(matrix) + fmt.Printf("%+v\n", matrix) + + matrix = [][]int{{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}} + setZeroes(matrix) + fmt.Printf("%+v\n", matrix) +} diff --git a/top-100-liked/19/19_test.go b/top-100-liked/19/19_test.go new file mode 100644 index 0000000..c9d0eec --- /dev/null +++ b/top-100-liked/19/19_test.go @@ -0,0 +1,84 @@ +package top100liked + +import ( + "fmt" + "testing" +) + +// https://leetcode.cn/problems/spiral-matrix/description/?envType=study-plan-v2&envId=top-100-liked + +func spiralOrder(matrix [][]int) []int { + rowLength := len(matrix) + columeLength := len(matrix[0]) + // 0 表示从左到右,1 表示从上到下,2 表示从右往左,3 表示从下到上 + direction := 0 + count := 0 + path := make([][]int, len(matrix)) + for i := range path { + path[i] = make([]int, len(matrix[0])) + } + + r, c := 0, 0 + result := []int{} + for count < len(matrix)*len(matrix[0]) { + switch direction { + case 0: + for ; c < columeLength; c++ { + if path[r][c] == 1 { + break + } + path[r][c] = 1 + result = append(result, matrix[r][c]) + count++ + } + r++ + c-- + direction = 1 + case 1: + for ; r < rowLength; r++ { + if path[r][c] == 1 { + break + } + path[r][c] = 1 + result = append(result, matrix[r][c]) + count++ + } + c-- + r-- + direction = 2 + case 2: + for ; c >= 0; c-- { + if path[r][c] == 1 { + break + } + path[r][c] = 1 + result = append(result, matrix[r][c]) + count++ + } + r-- + c++ + direction = 3 + case 3: + for ; r >= 0; r-- { + if path[r][c] == 1 { + break + } + path[r][c] = 1 + result = append(result, matrix[r][c]) + count++ + } + c++ + r++ + direction = 0 + } + } + return result +} + +func TestS19(t *testing.T) { + var matrix [][]int + matrix = [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} + fmt.Printf("%+v\n", spiralOrder(matrix)) + matrix = [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} + fmt.Printf("%+v\n", spiralOrder(matrix)) +} diff --git a/top-100-liked/2_test.go b/top-100-liked/2/2_test.go similarity index 100% rename from top-100-liked/2_test.go rename to top-100-liked/2/2_test.go diff --git a/top-100-liked/20/20_test.go b/top-100-liked/20/20_test.go new file mode 100644 index 0000000..07c5561 --- /dev/null +++ b/top-100-liked/20/20_test.go @@ -0,0 +1,30 @@ +package top100liked + +import ( + "fmt" + "testing" +) + +// https://leetcode.cn/problems/rotate-image/?envType=study-plan-v2&envId=top-100-liked + +func rotate(matrix [][]int) { + n := len(matrix) + for i := range (n-1)/2 + 1 { + for j := i; j < max(j, n-i-1); j++ { + tmp := matrix[i][j] + for range 4 { + matrix[j][n-i-1], tmp = tmp, matrix[j][n-i-1] + i, j = j, n-i-1 + } + } + } +} + +func TestS20(t *testing.T) { + matrix := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} + rotate(matrix) + fmt.Printf("%+v\n", matrix) + matrix = [][]int{{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}} + rotate(matrix) + fmt.Printf("%+v\n", matrix) +} diff --git a/top-100-liked/21/21_test.go b/top-100-liked/21/21_test.go new file mode 100644 index 0000000..fb42bd6 --- /dev/null +++ b/top-100-liked/21/21_test.go @@ -0,0 +1,44 @@ +package top100liked + +import ( + "fmt" + "testing" +) + +func searchMatrix(matrix [][]int, target int) bool { + for i := range len(matrix) { + for j := range len(matrix[0]) { + if matrix[i][j] == target { + return true + } + if j == 0 && matrix[i][j] > target { + return false + } + if matrix[i][j] > target { + break + } + } + } + return false +} + +// func searchMatrix(matrix [][]int, target int) bool { +// m, n := len(matrix), len(matrix[0]) +// x, y := 0, n-1 +// for x < m && y >= 0 { +// if matrix[x][y] == target { +// return true +// } +// if matrix[x][y] > target { +// y-- +// } else { +// x++ +// } +// } +// return false +// } + +func TestS21(t *testing.T) { + fmt.Println(searchMatrix([][]int{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}, 5)) + fmt.Println(searchMatrix([][]int{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}, 20)) +} diff --git a/top-100-liked/3_test.go b/top-100-liked/3/3_test.go similarity index 100% rename from top-100-liked/3_test.go rename to top-100-liked/3/3_test.go diff --git a/top-100-liked/4_test.go b/top-100-liked/4/4_test.go similarity index 100% rename from top-100-liked/4_test.go rename to top-100-liked/4/4_test.go diff --git a/top-100-liked/5_test.go b/top-100-liked/5/5_test.go similarity index 100% rename from top-100-liked/5_test.go rename to top-100-liked/5/5_test.go diff --git a/top-100-liked/6_test.go b/top-100-liked/6/6_test.go similarity index 100% rename from top-100-liked/6_test.go rename to top-100-liked/6/6_test.go diff --git a/top-100-liked/7_test.go b/top-100-liked/7/7_test.go similarity index 100% rename from top-100-liked/7_test.go rename to top-100-liked/7/7_test.go diff --git a/top-100-liked/8_test.go b/top-100-liked/8/8_test.go similarity index 100% rename from top-100-liked/8_test.go rename to top-100-liked/8/8_test.go diff --git a/top-100-liked/9_test.go b/top-100-liked/9/9_test.go similarity index 100% rename from top-100-liked/9_test.go rename to top-100-liked/9/9_test.go