Skip to content

401. 二进制手表 #40

Open
Open
@zpc7

Description

@zpc7

401. 二进制手表

/**
 * @param {number} turnedOn
 * @return {string[]}
 */
var readBinaryWatch = function (turnedOn) {
  const ans = [];
  if (turnedOn > 8) return ans;

  const getBinaryCount = num => num.toString(2).split('').filter(item => item !== '0').length;

  for (let h = 0; h < 12; h++) {
    for (let m = 0; m < 60; m++) {
      if (getBinaryCount(h) + getBinaryCount(m) === turnedOn) {
        ans.push(h + ":" + (m < 10 ? "0" : "") + m);
      }
    }
  }
  return ans;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    简单LeetCode 难度定级

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions