Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 589 Bytes

401. 二进制手表.md

File metadata and controls

32 lines (25 loc) · 589 Bytes

代码

var readBinaryWatch = function(num) {
  const Hour = [1, 2, 4, 8, 0, 0, 0, 0, 0, 0]
  const Min = [0, 0, 0, 0, 1, 2, 4, 8, 16, 32]
  const res = []

  traceBack(num, 0, 0, 0)
  return res

  function traceBack(num, index, h, m) {
    if (h > 11 || m > 59) return;
    if (num == 0) {
      let str = ''
      str += `${h}:${m > 9 ? m : '0'+m}`
      res.push(str)
      return
    }
    for(let i = index; i < 10; i ++) {
      traceBack(num-1, i+1, h+Hour[i], m+Min[i])
    }
  }
};

复杂度分析

时间复杂度 $O(nlogN)$

空间复杂度 $O(1)$