leetcode--401. Binary Watch

1、问题描述

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".

2、边界条件:1、num比较大的情况,在前面就可以处理掉,后面就可以不用考虑了;2、num = 0的情况;3、num<0不考虑,其他也在hour的循环里面过滤掉了。

3、思路:1、先确定hour的亮灯数,for循环;2、num - hour即为min的亮灯数,校验一下合法性;3、用递归的方法,分别求出hour和min的亮灯组合;4、最后将hour和min组合起来。

递归子函数思路:1、来到一个位置,有两种选择-亮灯或者不亮灯;2、选择不亮灯,亮灯数不变,index+1,但是有限制条件--剩余位置>剩余亮灯数,否则必须选择亮灯;3、选择亮灯,亮灯数-1,index+1;4、进入下一层。

base case:亮灯数==0,1、把int[] 记录的亮灯分布,计算成时/分;2、hour不用多处理,直接转为String保存,min需要判断>9?决定是否添0。

4、代码实现

class Solution {
    public List<String> readBinaryWatch(int num) {
        List<String> results = new ArrayList<>();
        for (int hourLedOnNum = 0; hourLedOnNum < 4 && hourLedOnNum <= num; hourLedOnNum++) {
            int minLedOnNum = num - hourLedOnNum;
            if (minLedOnNum >= 6) {
                continue;
            }
            List<String> hours = new ArrayList<>();
            int[] hourLeds = {0, 0, 0, 0};
            timeCombs(hours, hourLeds, hourLedOnNum, 0);

            List<String> mins = new ArrayList<>();
            int[] minLeds = {0, 0, 0, 0, 0, 0};
            timeCombs(mins, minLeds, minLedOnNum, 0);

            results.addAll(watchCombs(hours, mins));
        }
        return results;
    }

    public void timeCombs(List<String> times, int[] leds, int ledOnNum, int index) {
        if (ledOnNum == 0 || index == leds.length) {//后面条件可以去掉
            //record reuslts
            int time = 0;
            for (int i = 0; i < leds.length; i++) {
                time += leds[i] << i;
            }
            if ((leds.length == 4 && time > 11)
                || (leds.length == 6 && time > 59)) {
                return;
            }
            if (leds.length == 6 && time < 10) {//min小于10需要加0
                times.add("0" + Integer.toString(time));
            } else {
                times.add(Integer.toString(time));//Integer.toString(int i) int i 转成 String
            }
            return;
        }
        if (ledOnNum < leds.length - index) {
            timeCombs(times, leds, ledOnNum, index + 1); //not choose
        }
        leds[index] = 1; //choose
        timeCombs(times, leds, ledOnNum - 1, index + 1);
        leds[index] = 0;
    }

    public List<String> watchCombs(List<String> hours, List<String> mins) {
        List<String> results = new ArrayList<>();
        for (int i = 0; i < hours.size(); i++) {
            for (int j = 0; j < mins.size(); j++) {
                results.add(hours.get(i) + ":" + mins.get(j));
            }
        }
        return results;
    }
}

5、时间复杂度:; 空间复杂度:

6、主要api:

int类型转成String:String Integer.toString(int i),i是被转换,返回值为StringInteger类型转为String: Integer i = new Integer(); String str = i.toString();类似的还有 Character.toString(char c); Character ch = new Character(); ch.toString();
7、其他解法:从结果上面筛选,数一下bit数
    public List<String> readBinaryWatch(int num) {
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 60; j++) {
                if (Integer.bitCount(i) + Integer.bitCount(j) == num) {
                    result.add(String.format("%d:%02d", i, j));
                }
            }
        }
        return result;
    }
 
时间: 2024-10-14 20:26:23

leetcode--401. Binary Watch的相关文章

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode: Validata Binary Search Tree

LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree o

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space s

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-

[LeetCode] Unique Binary Search Trees II (难以忍受的递归)

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 class Solution { private