/**
* @param {number[][]} heights
* @return {number[][]}
*/
var pacificAtlantic = function(heights) {
//如果二維陣列不存在返回
if(!heights || !heights[0]) return []
const m = heights.length
const n = heights[0].length
//二維陣列創法
const flow1 = Array.from({length:m}, ()=>{new Array(n).fill(false)})
const flow2 = Array.from({length:m}, ()=>{new Array(n).fill(false)})
const dfs = (r,c, flow) => {
flow[r][c] = true
[[r+1, c],[r-1, c], [r,c+1],[r, c-1]].forEach(([nr,nc]) => {
if(nr>=0 && nr < m && nc>=0 && nc<n &&
//防止死循環
!flow[nr][nc]&&
//保證逆流而上
heights[nr][nc] >= heights[n][c]
)
dfs(nc,nr, flow)
})
}
//沿著海岸線開始圖的深度遍歷
for(let i = 0; i< m ; i++){
dfs(i, 0 , flow1)
dfs(i,n-1, flow2 )
}
for(let i =0; i<n; i++){
dfs(0, i ,flow1)
dfs(m-1, i ,flow2)
}
let res = []
for(let i =0 ; i <m ; i++){
for(let j =0 ; j < n; j++){
if(flow1[i][j] && flow2[i][j]){
res.push([r,c])
}
}
}
return res
};
是哪裡有問題呢? 我看 [[r+1, c],[r-1, c], [r,c+1],[r, c-1]]有值呀