码迷,mamicode.com
首页 > 其他好文 > 详细

401. Binary Watch

时间:2020-01-16 14:44:07      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:watch   wiki   数组   problems   private   val   led   bsp   discus   

A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (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 integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.

Example:

Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]

 

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".
public class Solution {
    public List<String> readBinaryWatch(int num) {
        List<String> res = new ArrayList<>();
        int[] nums1 = new int[]{8, 4, 2, 1}, nums2 = new int[]{32, 16, 8, 4, 2, 1};
        for(int i = 0; i <= num; i++) {
            List<Integer> list1 = generateDigit(nums1, i);
            List<Integer> list2 = generateDigit(nums2, num - i);
            for(int num1: list1) {
                if(num1 >= 12) continue;
                for(int num2: list2) {
                    if(num2 >= 60) continue;
                    res.add(num1 + ":" + (num2 < 10 ? "0" + num2 : num2));
                }
            }
        }
        return res;
    }

    private List<Integer> generateDigit(int[] nums, int count) {
        List<Integer> res = new ArrayList<>();
        generateDigitHelper(nums, count, 0, 0, res);
        return res;
    }

    private void generateDigitHelper(int[] nums, int count, int pos, int sum, List<Integer> res) {
        if(count == 0) {
            res.add(sum);
            return;
        }
        
        for(int i = pos; i < nums.length; i++) {
            generateDigitHelper(nums, count - 1, i + 1, sum + nums[i], res);    
        }
    }
}

https://leetcode.com/problems/binary-watch/discuss/88456/3ms-Java-Solution-Using-Backtracking-and-Idea-of-%22Permutation-and-Combination%22

这答案看得我都要站起来鼓掌了

思路:

既然是显示时间,那就分成hour和minute。一共要亮num位的话,设hour亮i位,那minute就该亮num-i位。

然后create两个list分别存放当前对应i和num-i的组合们,比如num=3,hour=1,minute=2,list1和list2分别代表hour亮1位可能产生的结果和minute亮2位可能产生的结果。

然后经过判断进行组合,hour当然要<=12,minute要<=60,同时minute<10的时候要加个0充当十位。

具体的dfs就是普通的combination,有两个method,其中一个生成list,另一个生成list中的element(path)。

count----还剩下多少位,等于0时得到一个解

pos------nums数组对应的位置,当前的用了后+1

sum-----nums元素之和,作为hour或minute的备选项。

401. Binary Watch

标签:watch   wiki   数组   problems   private   val   led   bsp   discus   

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12200668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!