u
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user