mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-06-04 02:21:15 +08:00
routine
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user