HZFE / algorithms-solving

1 stars 0 forks source link

2022-09-02 #35

Open github-actions[bot] opened 2 years ago

Akiq2016 commented 2 years ago
/*
 * @lc app=leetcode.cn id=36 lang=javascript
 *
 * [36] 有效的数独
 */

// @lc code=start
/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function (board) {
  const rowMap = {};
  const colMap = {};
  const blockMap = {};

  for (let col = 0; col < 9; col++) {
    if (!colMap[col]) {
      colMap[col] = {};
    }

    for (let row = 0; row < 9; row++) {
      const blockIdx = `${Math.floor(row / 3)},${Math.floor(col / 3)}`;

      if (!rowMap[col]) {
        rowMap[col] = {};
      }

      if (!blockMap[blockIdx]) {
        blockMap[blockIdx] = {};
      }

      const curVal = board[col][row];

      if (Number.isNaN(+curVal)) {
        continue;
      }

      if (
        rowMap[row][curVal] ||
        colMap[col][curVal] ||
        blockMap[blockIdx][curVal]
      ) {
        return false;
      }

      rowMap[row][curVal] = true;
      colMap[col][curVal] = true;
      blockMap[blockIdx][curVal] = true;
    }
  }

  return true;
};
// @lc code=end
gongpeione commented 2 years ago

191 Number of 1 Bits

/*
 * @lc app=leetcode id=191 lang=typescript
 *
 * [191] Number of 1 Bits
 */

// @lc code=start
function hammingWeight(n: number): number {
    let count = 0;

    while (n) {
        if (n & 1) {
            count++;
        }
        n = n >>> 1;
    }

    return count;
};
// @lc code=end

Nickname: Geeku From vscode-hzfe-algorithms

gongpeione commented 2 years ago

191 Number of 1 Bits

/*
 * @lc app=leetcode id=191 lang=typescript
 *
 * [191] Number of 1 Bits
 */

// @lc code=start
function hammingWeight(n: number): number {
    let count = 0;

    while (n) {
        if (n & 1) {
            count++;
        }
        n = n >>> 1;
    }

    return count;
};
// @lc code=end

Nickname: Geeku From vscode-hzfe-algorithms