JS为什么整数变量被重置为初始值但数组变量不是?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS为什么整数变量被重置为初始值但数组变量不是?相关的知识,希望对你有一定的参考价值。
给定矩阵/图形/多维数组的输入(不确定它们在编程中是否具有唯一的定义),下面的函数应该输出最大岛的大小。土地用1表示,水用0表示,两个区域只有在水平或垂直(不是对角线)相邻时才连接。
下面的算法有效,但只有当我保存数组中的所有岛时,才是“islands”变量,然后返回保存的最大值。
最初,我试图仅用“最大”变量跟踪最大值的大小。但是,由于某种原因,它会一直重置为初始值0。
为什么会这样?我特别困惑的是,为什么这可能发生在“最大”变量而不是“孤岛”变量,因为两者似乎具有完全相同的范围。
const bigIsland = matrix => {
let biggest = 0
let islands = []
let visited = matrix.map(row => row.map(node => false))
for(let y=0; y<matrix.length; y++){
for(let x=0; x<matrix[y].length; x++){
if (visited[y][x]) continue
checkIsland(matrix, visited, y, x, islands, biggest)
}
}
return Math.max(...islands)
}
const checkIsland = (matrix, visited, y, x, islands, biggest) => {
console.log("biggestStart", biggest)
console.log("islandsStart", islands)
let currIsland = 0
let nodesToCheck = [[y,x]]
while(nodesToCheck.length){
const [y, x] = nodesToCheck.pop()
if (visited[y][x]) continue
visited[y][x] = true
if (matrix[y][x]===0) continue
currIsland++
console.log("currIsland", currIsland)
getValidNeighbors(matrix, visited, y, x, nodesToCheck)
}
if (currIsland > biggest) biggest = currIsland
if (currIsland > 0) islands.push(currIsland)
console.log("biggest", biggest)
console.log("islands", islands)
}
const getValidNeighbors = (matrix, visited, y, x, nodesToCheck) => {
if(y>0) nodesToCheck.push([y-1, x])
if(y<matrix.length-1) nodesToCheck.push([y+1, x])
if(x>0) nodesToCheck.push([y, x-1])
if(x<matrix[y].length-1) nodesToCheck.push([y, x+1])
}
bigIsland([ [1, 0, 1],
[1, 0, 1],
[1, 0, 0] ])
答案
将原始变量传递给函数(例如biggest
到checkIsland
)时,该函数中的任何内容都不能更改调用函数中对该变量的原始引用。为了改变biggest
中的bigIsland
,你需要明确地从biggest
返回找到的checkIsland
:
const bigIsland = matrix => {
let biggest = 0
let islands = []
let visited = matrix.map(row => row.map(node => false))
for(let y=0; y<matrix.length; y++){
for(let x=0; x<matrix[y].length; x++){
if (visited[y][x]) continue
biggest = checkIsland(matrix, visited, y, x, islands, biggest)
}
}
return Math.max(...islands)
}
const checkIsland = (matrix, visited, y, x, islands, biggest) => {
console.log("biggestStart", biggest)
console.log("islandsStart", islands)
let currIsland = 0
let nodesToCheck = [[y,x]]
while(nodesToCheck.length){
const [y, x] = nodesToCheck.pop()
if (visited[y][x]) continue
visited[y][x] = true
if (matrix[y][x]===0) continue
currIsland++
console.log("currIsland", currIsland)
getValidNeighbors(matrix, visited, y, x, nodesToCheck)
}
if (currIsland > biggest) biggest = currIsland
if (currIsland > 0) islands.push(currIsland)
console.log("biggest", biggest)
console.log("islands", islands)
return biggest;
}
const getValidNeighbors = (matrix, visited, y, x, nodesToCheck) => {
if(y>0) nodesToCheck.push([y-1, x])
if(y<matrix.length-1) nodesToCheck.push([y+1, x])
if(x>0) nodesToCheck.push([y, x-1])
if(x<matrix[y].length-1) nodesToCheck.push([y, x+1])
}
bigIsland([ [1, 0, 1],
[1, 0, 1],
[1, 0, 0] ])
另一答案
函数biggest
中的变量bigIsland
独立于biggest
中的变量checkIsland
。在一个函数中更改变量对另一个函数没有影响。数组是不同的,因为当您传递数组时,您将获得对变量中相同数组的引用。考虑:
function one() {
let variable = 0;
console.log("variable in on before calling two ", variable)
two(variable)
console.log("variable in on after calling two ", variable)
}
function two(variable) {
variable += 200
console.log("variable in two", variable)
}
one()
以上是关于JS为什么整数变量被重置为初始值但数组变量不是?的主要内容,如果未能解决你的问题,请参考以下文章