Ruby's Louvre

每天学习一点点算法

导航

leetcode 36. Valid Sudoku

数独


 function isValidSudoku(board) {
      var rows = []//行
      var cols = [] // 列
      var cubes = []//9宫格
      for (var i = 0; i < 9; i++) {
        rows.push({})
        cols.push({})
        cubes.push({})
      }
      for (var i = 0; i < 9; i++) { //循环行
        for (var j = 0; j < 9; j++) {//循环列
          var value = board[i][j]
          if (value != '.') {
            var cubeIndex = 3 * Math.trunc(i / 3) + Math.trunc(j / 3)// 0-8
            if (rows[i][value] || cols[j][value] || cubes[cubeIndex][value]) return false;
            rows[i][value] = true
            cols[j][value] = true
            cubes[cubeIndex][value] = true
          }
        }
      }
      return true;
    }

posted on 2019-12-15 14:53  司徒正美  阅读(156)  评论(0编辑  收藏  举报