566. 重塑矩阵

思路

1、先看需要变换的矩阵元素是不是超过原矩阵 或者 行列没有发生变化,直接返回原矩阵
2、将原矩阵的行列顺序按照列数重排

代码

  1. /**
  2. * @param {number[][]} nums
  3. * @param {number} r
  4. * @param {number} c
  5. * @return {number[][]}
  6. */
  7. var matrixReshape = function(nums, r, c) {
  8. const rowLen = nums.length, colLen = nums[0] && nums[0].length;
  9. if (r * c > rowLen * colLen || (r === rowLen && c === colLen)) return nums;
  10. nums = nums.flat();
  11. const ret = []
  12. let path = []
  13. for(let i = 0; i < nums.length; i ++) {
  14. path.push(nums[i])
  15. if ((i+1) % c === 0) {
  16. ret.push(path)
  17. path = []
  18. }
  19. }
  20. return ret;
  21. };

复杂度分析

时间复杂度 二维数组遍历 - 图1#card=math&code=O%28r%2Ac%29)
空间复杂度 二维数组遍历 - 图2#card=math&code=O%28c%29)

48. 旋转图像

思路

1、先复制一个副本
2、找出旋转之后的坐标对应关系 row = len - col - 1, col = row

代码

  1. /**
  2. * @param {number[][]} matrix
  3. * @return {void} Do not return anything, modify matrix in-place instead.
  4. */
  5. var rotate = function(matrix) {
  6. const copy = matrix.map(item => item.slice())
  7. const len = matrix.length
  8. for(let i = 0; i < len; i ++) {
  9. for(let j = 0; j < len; j ++) {
  10. matrix[i][j] = copy[len - j - 1][i];
  11. }
  12. }
  13. return matrix
  14. };

复杂度分析

时间复杂度 二维数组遍历 - 图3#card=math&code=O%28N%5E2%29)
空间复杂度 二维数组遍历 - 图4#card=math&code=O%28N%5E2%29)

73. 矩阵置零

思路

1、先遍历一遍,找到有0 的行和列
2、再遍历,将有0的行和列置0

代码

  1. /**
  2. * @param {number[][]} matrix
  3. * @return {void} Do not return anything, modify matrix in-place instead.
  4. */
  5. var setZeroes = function(matrix) {
  6. const rowSet = new Set(), colSet = new Set();
  7. const rowLen = matrix.length, colLen = matrix[0] && matrix[0].length;
  8. for(let i = 0; i < rowLen; i ++) {
  9. for(let j = 0; j < colLen; j ++) {
  10. if (matrix[i][j] === 0) {
  11. rowSet.add(i)
  12. colSet.add(j)
  13. }
  14. }
  15. }
  16. for(let i = 0; i < rowLen; i ++) {
  17. for(let j = 0; j < colLen; j ++) {
  18. if (rowSet.has(i) || colSet.has(j)) {
  19. matrix[i][j] = 0
  20. }
  21. }
  22. }
  23. return matrix
  24. };

复杂度分析

时间复杂度 二维数组遍历 - 图5#card=math&code=O%28row%2Acol%29)
空间复杂度 二维数组遍历 - 图6#card=math&code=O%28row%20%2B%20col%29)

289. 生命游戏

思路

1、创建一个副本
2、计算当前位置的生命状态

代码

  1. /**
  2. * @param {number[][]} board
  3. * @return {void} Do not return anything, modify board in-place instead.
  4. */
  5. var gameOfLife = function(board) {
  6. // 创建副本
  7. const copy = board.map(item => item.slice());
  8. const rowLen = board.length, colLen = board[0].length;
  9. // 遍历
  10. for(let i = 0; i < rowLen; i ++) {
  11. for(let j = 0; j < colLen; j ++) {
  12. board[i][j] = calcLifeState(copy, i, j);
  13. }
  14. }
  15. return board;
  16. };
  17. // 计算当前细胞的生命状态
  18. function calcLifeState(matrix, rowIndex, colIndex) {
  19. let sum = 0, rowLen = matrix.length, colLen = matrix[0].length;
  20. for(let row = Math.max(rowIndex-1, 0); row <= Math.min(rowIndex+1, rowLen-1); row ++) {
  21. for(let col = Math.max(colIndex-1, 0); col <= Math.min(colIndex+1, colLen-1); col ++) {
  22. if (!(row === rowIndex && col === colIndex)) {
  23. sum+= matrix[row][col] || 0
  24. }
  25. }
  26. }
  27. // 当前细胞为活细胞
  28. if (matrix[rowIndex][colIndex]) {
  29. if (sum < 2 || sum > 3) return 0;
  30. return 1
  31. }
  32. // 当前为死细胞
  33. if (sum === 3) return 1;
  34. return 0
  35. }

复杂度分析

时间复杂度 二维数组遍历 - 图7#card=math&code=O%28row%20%2A%20col%29)
空间复杂度 二维数组遍历 - 图8#card=math&code=O%28row%20%2A%20col%29)