英文:
Invalid operation: index of type *int golang
问题
**目标:**我一直在使用Go解决《破解编程面试》一书中的第6题。
注意:我不需要这个问题的帮助或解决方案
给定一个由NxN矩阵表示的图像,图像中的每个像素占据4个字节,编写一个方法将图像旋转90度。你能原地完成吗?
**问题:**我创建了一个数组的数组来表示矩阵,并创建了一个交换函数来顺时针交换矩阵中的元素。但是出现了一个非常奇怪的错误,当我尝试编译时:
./Q6.go:29: invalid operation: b[N - col - 1] (index of type *int)
./Q6.go:30: invalid operation: b[N - row - 1] (index of type *int)
我从哪里得到了类型为int的索引?在Go文档中,len(v)返回类型为int,而其他所有值都是类型为'int'的,那么我是如何得到类型为int的索引的?
代码:
package main
import "fmt"
func main() {
b := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} // 4 by 4 array going from 1 to 16
N := len(b)
for row := 0; row < N / 2; row++ {
for col := row; col < N - row - 1; col++ {
a := &b[row][col]
b := &b[col][N - row - 1]
c := &b[N - col - 1][col] // <-- 错误在这里
d := &b[N - row - 1][N - col - 1] // <-- 错误在这里
fourSwap(a, b, c, d)
}
}
for r := range b {
for c:= range b[0] {
fmt.Print(b[r][c])
}
fmt.Print("\n")
}
}
// [a][-][-][b] [c][-][-][a]
// [-][-][-][-] --> [-][-][-][-]
// [-][-][-][-] --> [-][-][-][-]
// [c][-][-][d] [d][-][-][b]
func fourSwap(a, b, c, d *int) {
temp := *b
*b = *a
*a = *c
*c = *d
*d = temp
}
英文:
Objective: I have been solving question 6 from the book 'Cracking the Coding interview' by using Go.
NOTE I DO NOT WAN'T HELP OR SOLUTIONS TO THIS QUESTION
Given an image represented by an NxN matrix, where each pixel in the image is 4
bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
Problem: I made an array of arrays to represent the matrix and I created a swap function to swap the elements clockwise in the matrix. For some reason I get this really weird error when trying to compile:
./Q6.go:29: invalid operation: b[N - col - 1] (index of type *int)
./Q6.go:30: invalid operation: b[N - row - 1] (index of type *int)
Where am I getting type *int as an index? In the Go documenation, len(v) returns type int and everything else is in the value of 'N - col - 1' is type int so how am I getting type *int index?
Code:
package main
import "fmt"
func main() {
b := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} // 4 by 4 array going from 1 to 16
N := len(b)
for row := 0; row < N / 2; row++ {
for col := row; col < N - row - 1; col++ {
a := &b[row][col]
b := &b[col][N - row - 1]
c := &b[N - col - 1][col] // <-- Error here
d := &b[N - row - 1][N - col - 1] // <-- Error here
fourSwap(a, b, c, d)
}
}
for r := range b {
for c:= range b[0] {
fmt.Print(b[r][c])
}
fmt.Print("\n")
}
}
// [a][-][-][b] [c][-][-][a]
// [-][-][-][-] --> [-][-][-][-]
// [-][-][-][-] --> [-][-][-][-]
// [c][-][-][d] [d][-][-][b]
func fourSwap(a, b, c, d *int) {
temp := *b
*b = *a
*a = *c
*c = *d
*d = temp
}
答案1
得分: 2
你在循环内部声明了b
,这就遮蔽了你的切片。
for row := 0; row < N / 2; row++ {
for col := row; col < N - row - 1; col++ {
a := &b[row][col]
b := &b[col][N - row - 1] <<< b现在是一个*int
c := &b[N - col - 1][col] // <-- 这里有错误
d := &b[N - row - 1][N - col - 1] // <-- 这里有错误
fourSwap(a, b, c, d)
}
}
英文:
You declare b
inside the loop, and that shadows your slice.
for row := 0; row < N / 2; row++ {
for col := row; col < N - row - 1; col++ {
a := &b[row][col]
b := &b[col][N - row - 1] <<<< b is now an *int
c := &b[N - col - 1][col] // <-- Error here
d := &b[N - row - 1][N - col - 1] // <-- Error here
fourSwap(a, b, c, d)
}
}
答案2
得分: 1
你在出错的那一行之前创建了一个新的本地变量b,它是一个指针:
b := &b[col][N - row - 1]
英文:
You are creating a new local variable b which is a pointer on the line before you get the error:
b := &b[col][N - row - 1]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论