mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-07-18 03:12:22 +08:00
Compare commits
6
Commits
84921163ec
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb44fe2957 | ||
|
|
39954c4a80 | ||
|
|
4355946f39 | ||
|
|
45faf83d37 | ||
|
|
6339943b70 | ||
|
|
be9ecb423a |
@@ -0,0 +1,12 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func reverseSubmatrix(grid [][]int, x int, y int, k int) [][]int {
|
||||||
|
for i := 0; i < k/2; i++ {
|
||||||
|
for j := range k {
|
||||||
|
temp := grid[x+k-1-i][y+j]
|
||||||
|
grid[x+k-1-i][y+j] = grid[x+i][y+j]
|
||||||
|
grid[x+i][y+j] = temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return grid
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMinDistance(nums []int, target int, start int) int {
|
||||||
|
ans := math.MaxInt
|
||||||
|
for i, v := range nums {
|
||||||
|
if v == target {
|
||||||
|
ans = min(abs(i-start), ans)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
// 旋转90度通用方法就是 先水平旋转,然后转置
|
||||||
|
func findRotation(mat [][]int, target [][]int) bool {
|
||||||
|
n := len(mat)
|
||||||
|
// 最多旋转 4 次
|
||||||
|
for k := 0; k < 4; k++ {
|
||||||
|
// 旋转操作
|
||||||
|
for i := 0; i < n/2; i++ {
|
||||||
|
for j := 0; j < (n+1)/2; j++ {
|
||||||
|
mat[i][j], mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i] =
|
||||||
|
mat[n-1-j][i], mat[n-1-i][n-1-j], mat[j][n-1-i], mat[i][j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if isEqual(mat, target) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEqual(mat, target [][]int) bool {
|
||||||
|
n := len(mat)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
for j := 0; j < n; j++ {
|
||||||
|
if mat[i][j] != target[i][j] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
func abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func closestTarget(words []string, target string, startIndex int) int {
|
||||||
|
ans := len(words)
|
||||||
|
n := len(words)
|
||||||
|
|
||||||
|
for i, word := range words {
|
||||||
|
if word == target {
|
||||||
|
dist := abs(i - startIndex)
|
||||||
|
ans = min(ans, min(dist, n-dist))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ans < n {
|
||||||
|
return ans
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func minimumDistance(nums []int) int {
|
||||||
|
m := make(map[int]([]int))
|
||||||
|
for i, v := range nums {
|
||||||
|
_, ok := m[v]
|
||||||
|
if !ok {
|
||||||
|
m[v] = make([]int, 0)
|
||||||
|
}
|
||||||
|
m[v] = append(m[v], i)
|
||||||
|
}
|
||||||
|
ans := math.MaxInt
|
||||||
|
for _, v := range m {
|
||||||
|
if len(v) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for i := 0; i+2 < len(v); i++ {
|
||||||
|
temp := abs(v[i]-v[i+1]) + abs(v[i]-v[i+2]) + abs(v[i+1]-v[i+2])
|
||||||
|
ans = min(ans, temp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ans == math.MaxInt {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package A
|
||||||
|
|
||||||
|
type node1559 struct {
|
||||||
|
x, y int
|
||||||
|
px, py int
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsCycle(grid [][]byte) bool {
|
||||||
|
m := len(grid)
|
||||||
|
n := len(grid[0])
|
||||||
|
if m < 2 || n < 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
visited := make([][]bool, m)
|
||||||
|
for i := range visited {
|
||||||
|
visited[i] = make([]bool, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
dirs := [][2]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
|
||||||
|
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
for j := 0; j < n; j++ {
|
||||||
|
if visited[i][j] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
visited[i][j] = true
|
||||||
|
stack := []node1559{{x: i, y: j, px: -1, py: -1}}
|
||||||
|
|
||||||
|
for len(stack) > 0 {
|
||||||
|
top := stack[len(stack)-1]
|
||||||
|
stack = stack[:len(stack)-1]
|
||||||
|
|
||||||
|
for _, dir := range dirs {
|
||||||
|
nx := top.x + dir[0]
|
||||||
|
ny := top.y + dir[1]
|
||||||
|
if nx < 0 || nx >= m || ny < 0 || ny >= n {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if grid[nx][ny] != grid[top.x][top.y] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !visited[nx][ny] {
|
||||||
|
visited[nx][ny] = true
|
||||||
|
stack = append(stack, node1559{x: nx, y: ny, px: top.x, py: top.y})
|
||||||
|
} else if nx != top.px || ny != top.py {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user