This commit is contained in:
2026-03-27 21:48:51 +08:00
parent de39ce466a
commit 9b63dc45d3
3 changed files with 48 additions and 71 deletions
+17
View File
@@ -0,0 +1,17 @@
package main
func majorityElement(nums []int) int {
a, s := nums[0], 1
for i := 1; i < len(nums); i++ {
if a == nums[i] {
s++
continue
}
s--
if s == 0 {
a = nums[i]
s = 1
}
}
return a
}
+31
View File
@@ -0,0 +1,31 @@
package main
import "fmt"
func areSimilar(mat [][]int, k int) bool {
lenX := len(mat)
lenY := len(mat[0])
shift := k % lenY
for i := 0; i < lenX; i++ {
for j := 0; j < lenY; j++ {
next := 0
if i%2 == 0 {
next = (j + shift) % lenY
} else {
next = (j - shift + lenY) % lenY
}
if mat[i][j] != mat[i][next] {
return false
}
}
}
return true
}
func main() {
fmt.Println(areSimilar([][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 4))
fmt.Println(areSimilar([][]int{{1, 2, 1, 2}, {5, 5, 5, 5}, {6, 3, 6, 3}}, 2))
fmt.Println(areSimilar([][]int{{2, 2}, {2, 2}}, 3))
}