关于leetcode:200岛屿数量-算法leetcode附思维导图-全部解法300题
零 题目:算法(leetcode,附思维导图 + 全副解法)300题之(200)岛屿数量一 题目形容 二 解法总览(思维导图) 三 全副解法1 计划11)代码: // 计划1 “本人。模仿 - 标记法”。// 思路:// 1)状态初始化:m = grid.length, n = grid[0].length; 。// tempMap = new Map(), resMap = getMapByGrid(), resCount = 0; 。// 2)外围:循环解决 —— 条件为 存在未被拜访过的海洋 。// 3)返回后果 resCount 。var numIslands = function(grid) { const getMapByGrid = () => { let resMap = new Map(); for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { if (grid[i][j] === '1') { const tempVal = `${i}#${j}`; resMap.set(tempVal, 1); } } } return resMap; }; // 1)状态初始化:m = grid.length, n = grid[0].length; 。 // tempMap = new Map(), resMap = getMapByGrid(), resCount = 0; 。 const m = grid.length, n = grid[0].length; let // tempMap:目前已被拜访过的海洋。 tempMap = new Map(), resMap = getMapByGrid(), resCount = 0; // 2)外围:循环解决 —— 条件为 存在未被拜访过的海洋 。 while (resMap.size !== 0) { for (const [key, val] of resMap) { if (!tempMap.has(key)) { tempMap.set(key, 1); let tempQueue = [key]; while (tempQueue.length !== 0) { const key = tempQueue.shift(), [tempI, tempJ] = key.split('#').map(v => parseInt(v)); // 标记为已被拜访。 resMap.delete(key); // 4个方向的拜访。 // 上 if (tempI - 1 >= 0 && grid[tempI - 1][tempJ] === '1') { const key = `${tempI - 1}#${tempJ}`; if (!tempMap.has(key)) { tempQueue.push(key); tempMap.set(key, 1); } } // 下 if (tempI + 1 < m && grid[tempI + 1][tempJ] === '1') { const key = `${tempI + 1}#${tempJ}`; if (!tempMap.has(key)) { tempQueue.push(key); tempMap.set(key, 1); } } // 左 if (tempJ - 1 >= 0 && grid[tempI][tempJ - 1] === '1') { const key = `${tempI}#${tempJ - 1}`; if (!tempMap.has(key)) { tempQueue.push(key); tempMap.set(key, 1); } } // 右 if (tempJ + 1 < n && grid[tempI][tempJ + 1] === '1') { const key = `${tempI}#${tempJ + 1}`; if (!tempMap.has(key)) { tempQueue.push(key); tempMap.set(key, 1); } } } // 以后岛屿无奈再次连贯到任何海洋了。 resCount++; } break; } } // 3)返回后果 resCount 。 return resCount;};2 计划21)代码: ...