1 Star 0 Fork 0

catyMap / AlgorithmNote

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
findRotation.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
dogemap 提交于 2021-06-08 22:54 . 添加一些背包问题
package main
import (
"fmt"
"reflect"
)
func findRotation(mat [][]int, target [][]int) bool {
for i := 0 ; i < 4 ; i++ {
rotate(mat)
if reflect.DeepEqual(mat, target) {
return true
}
}
return false
}
func getCounts(grid [][]int) (res [][2]int) {
m := len(grid)
n := 0
if m > 0 {
n = len(grid[0])
}
up, down, left, right := 0, m-1, 0, n-1
zeroCount, oneCount := 0 ,0
for up <= down && left <= right {
// 上
for j := left ; j <= right ; j ++ {
if grid[up][j] == 0 {
zeroCount ++
}else {
oneCount ++
}
}
// 右
for i := up + 1 ; i <= down ; i ++ {
if grid[i][right] == 0 {
zeroCount ++
}else {
oneCount ++
}
}
if right - left + 1 > 1 && down - up + 1 > 0 {
// 下
for j := right - 1; j >= left ; j -- {
if grid[down][j] == 0{
zeroCount ++
}else {
oneCount ++
}
}
// 左
for i := down -1 ; i > up ; i -- {
if grid[i][left] == 0 {
zeroCount ++
}else {
oneCount ++
}
}
}
res = append(res, [2]int{zeroCount, oneCount})
zeroCount, oneCount = 0 , 0
up ++
down--
left++
right--
}
return res
}
func rotate(matrix [][]int) {
n := len(matrix)
// 水平翻转
for i := 0; i < n/2; i++ {
matrix[i], matrix[n-1-i] = matrix[n-1-i], matrix[i]
}
// 主对角线翻转
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
}
}
}
func main() {
fmt.Println(getCounts([][]int{{0,1},{1,0}}))
}
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

搜索帮助