英文:
How to exclude coordinates which overflow matrix
问题
任务是编写一个函数,该函数返回一个元素的所有邻居。对角线上的元素不被视为邻居。
一般情况下,问题已经解决,但如果元素超出了矩阵范围,编译器会生成运行时错误:索引超出范围[-1]。现在我正在考虑添加一个条件来排除矩阵外的数据。你能告诉我这样的条件应该是什么样子的吗?我已经尝试过类似这样的条件:如果元素等于-1,则从响应中删除元素。但它不起作用。
package main
import "fmt"
func Input(y, x int) []int {
matrix := [][]int{
[]int{0, 2, 3},
[]int{2, 3, 1},
[]int{8, 7, 4},
[]int{3, 2, 1},
}
k := []int{matrix[y+1][x], matrix[y-1][x], matrix[y][x+1], matrix[y][x-1]}
for _, z := range k {
if z == -1 { //error
return append(k[z:], k[:z-1]...)
}
}
return k
}
func main() {
fmt.Println(Input(0, 0))
}
请提供翻译后的代码部分。
英文:
The task is to write a function that returns all its neighbors for an element. Diagonal elements are not considered neighbors.
In general, the problem is solved, but if the elements fall outside the matrix, the compiler generates runtime error: index out of range [-1] . Now I'm thinking of making a condition that excludes data outside the matrix. Can you tell me how such a condition would look like? I've already tried something like: if element = -1, then remove element from response. But it doesn't work.
package main
import "fmt"
func Input(y, x int) []int {
matrix := [][]int{
[]int{0, 2, 3},
[]int{2, 3, 1},
[]int{8, 7, 4},
[]int{3, 2, 1},
}
k := []int{matrix[y+1][x], matrix[y-1][x], matrix[y][x+1], matrix[y][x-1]}
for _, z := range k {
if z == -1 { //error
return append(k[z:], k[:z-1]...)
}
}
return k
}
func main() {
fmt.Println(Input(0, 0))
}
答案1
得分: 1
x
和y
的索引都可能超出范围,不仅仅是当它们为-1
时,还包括它们大于等于你用它们进行索引的切片的长度。
所以基本上有4个邻居:
(y-1,x), (y+1, x), (y,x-1), (y,x+1)
你必须检查所有越界的情况。你的panic发生在k := ...
这一行,而不是在for
循环内部。同时,在循环中提前返回将无法处理所有元素,并且该for
循环从根本上就是错误的。
所以基本上你可以这样做:
var result []int
if y > 0 {
result = append(result, matrix[y-1][x])
}
if y < len(matrix)-1 {
result = append(result, matrix[y+1][x])
}
if x > 0 {
result = append(result, matrix[y][x-1])
}
if x < len(matrix[y])-1 {
result = append(result, matrix[y][x+1])
}
return result
这将输出(在Go Playground上尝试):
[2 2]
请注意,如果x
或y
已经无效,上述代码仍然会引发panic。如果你想防止这种情况发生,你还必须验证x
和y
,例如:
var result []int
if y < 0 || y >= len(matrix) || x < 0 || x >= len(matrix[y]) {
return result
}
另一种方法是在另一个切片中列出邻居的deltaY
和deltaX
索引:
var neighbors = [][]int{
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
}
然后你可以使用循环遍历neighbors
,然后可以在一个地方检查索引:
var result []int
for _, neighbor := range neighbors {
iy, ix := y+neighbor[0], x+neighbor[1]
if iy < 0 || iy >= len(matrix) || ix < 0 || ix >= len(matrix[iy]) {
continue
}
result = append(result, matrix[iy][ix])
}
return result
在Go Playground上尝试这个。
请注意,这个第二种解决方案也会返回邻居,如果你指定的索引超出了矩阵的范围,但那个元素的邻居仍然在矩阵内部。例如,使用y=-1
,x=0
作为输入,邻居(y+1,x)为0
将被返回。
英文:
Both x
and y
indices of neighbors may be out of range, and not just when being -1
, but also when they are >= length of the slice you index with them.
So basically there are 4 neighbors:
(y-1,x), (y+1, x), (y,x-1), (y,x+1)
You have to check all out of bounds cases. Your panic happens on the k := ...
line, not inside the for
loop. Also returning early in the loop will not handle all elements, and that for
is wrong from its roots.
So basically this is how you could do it:
var result []int
if y > 0 {
result = append(result, matrix[y-1][x])
}
if y < len(matrix)-1 {
result = append(result, matrix[y+1][x])
}
if x > 0 {
result = append(result, matrix[y][x-1])
}
if x < len(matrix[y])-1 {
result = append(result, matrix[y][x+1])
}
return result
This will output (try it on the Go Playground):
[2 2]
Note that if x
or y
is already invalid, the above code will still panic. If you want to protect against it, you also have to validate x
and y
, e.g.:
var result []int
if y < 0 || y >= len(matrix) || x < 0 || x >= len(matrix[y]) {
return result
}
Another approach is to list the deltaY
and deltaX
indices of neighbors in another slice:
var neighbors = [][]int{
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
}
And you may use a loop to iterate over neighbors
, and then you can check indices in a single place:
var result []int
for _, neighbor := range neighbors {
iy, ix := y+neighbor[0], x+neighbor[1]
if iy < 0 || iy >= len(matrix) || ix < 0 || ix >= len(matrix[iy]) {
continue
}
result = append(result, matrix[iy][ix])
}
return result
Try this one on the Go Playground.
Note that this second solution also returns neighbors if you specify indices outside of the matrix if the neighbor of that element falls inside the matrix. E.g. using y=-1
, x=0
as the input, the neighbor (y+1,x) being 0
will be returned.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论