401 Binary Watch
Last updated
Last updated
A binary watch has 4 LEDs on the top which represent thehours(0-11), and the 6 LEDs on the bottom represent theminutes(0-59).
Each LED represents a zero or one, with the least significant bit on the right.
For example, the above binary watch reads "3:25".
Given a non-negative integernwhich represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
Note:
The order of output does not matter.
The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
(1) Backtracking
思路: 根据给定的输入num,我们生成所有可能的hours和minutes值,其中要注意排除无效的值,比如hours必须小于12,minutes必须小于60
Backtracking: 时间复杂度O(12 * 60) => O(1), 空间复杂度O(12 * 60) => O(1)