LeetCode 401. Binary Watch

Posted wenyq7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 401. Binary Watch相关的知识,希望对你有一定的参考价值。

A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.

  • For example, the below binary watch reads "4:51".

Given an integer turnedOn which represents the number of LEDs that are currently on (ignoring the PM), return all possible times the watch could represent. You may return the answer in any order.

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

Example 1:

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

Example 2:

Input: turnedOn = 9
Output: []

Constraints:

  • 0 <= turnedOn <= 10

 这题是说用二进制来表示时间,要求如果整体的表示有n个1,有几种可能的组合。

就,拿到题就头大了。看了答案,一个比较简单的brute force的解法就是遍历所有的时间组合,计里面算一共有多少个1,如果h和m加起来1的个数为给定的数量,就加入result list。

class Solution 
    public List<String> readBinaryWatch(int turnedOn) 
        List<String> result = new ArrayList<>();
        for (int h = 0; h < 12; h++) 
            for (int m = 0; m < 60; m++) 
                if (Integer.bitCount(h) + Integer.bitCount(m) == turnedOn) 
                    result.add(h + ":" + ((m < 10) ? "0" : "") + m);
                
            
        
        return result;
    

 还有人说真正的industrial做法并不会搞什么高大上的东西,直接写死就好了……

 

- LeetCode

然后就是最令人头大的backtracking做法,着实是不会啊。我参考的是这篇:- LeetCode 

首先先用两个数组保存所有hour和min每个bit代表的数字(1/2/4/8...),于是就相当于从这些数字里面加起来凑成最后的hour和min。在一个for loop里面把turnedOn分为两个部分,一部分是hour(h),一部分是min(m),h + m = turnedOn。需要对hour和min分别生成 1的个数为h和m的数字,这里用一个helper来完成这个操作。拿到了hour和min对应的数字,只需要把他们都凑一起就行了。

在helper里,我们把candidate数字的数组、需要的1的个数、当前遍历到的position、这个数字的十进制表示、和一个用来存放满足1的个数的条件的数字的list。如果当前已经不需要新的1了,就把当前的十进制表示放进list。否则,我们就需要从当前的position开始,把当前position代表的那个数字设为1,直到设到了指定个数的1为止,就是前面的停止条件了。

这里我还额外加了一个参数,就是最大的数字限制,这样加入list的就只有满足条件的数字了。

理解的还不是很深刻,都是强行解释的答案……交了以后发现并没有比brute force快多少

class Solution 
    public List<String> readBinaryWatch(int turnedOn) 
        List<String> result = new ArrayList<>();
        int[] hours = new int[]8, 4, 2, 1;
        int[] mins = new int[]32, 16, 8, 4, 2, 1;

        for (int hourOne = 0; hourOne <= turnedOn; hourOne++) 
            int minOne = turnedOn - hourOne;
            System.out.println("hour: " + hourOne + ", min: " + minOne);
            System.out.println("generating hour");
            List<Integer> hourList = generateDigit(hours, hourOne, 12);
            System.out.println("generating min");
            List<Integer> minList = generateDigit(mins, minOne, 60);
            for (int hour : hourList) 
                for (int min : minList) 
                    result.add(hour + ":" + ((min < 10) ? "0" : "") + min);
                
            
        
        return result;
    

    private List<Integer> generateDigit(int[] array, int numOne, int max) 
        List<Integer> result = new ArrayList<>();
        helper(array, numOne, 0, 0, result, max);
        return result;
    

    private void helper(int[] array, int numOne, int pos, int number, List<Integer> result, int max) 
        System.out.println(numOne + ", " + pos + ", " + number);
        if (numOne == 0) 
            if (number < max) 
                System.out.println("added: " + number);
                result.add(number);
            
            return;
        

        for (int i = pos; i < array.length; i++) 
            helper(array, numOne - 1, i + 1, number + array[i], result, max);
        
    

举个例子:

turnedOn = 1

hour: 0, min: 1
generating hour
0, 0, 0
added: 0
generating min
1, 0, 0
0, 1, 32
added: 32
0, 2, 16
added: 16
0, 3, 8
added: 8
0, 4, 4
added: 4
0, 5, 2
added: 2
0, 6, 1
added: 1
hour: 1, min: 0
generating hour
1, 0, 0
0, 1, 8
added: 8
0, 2, 4
added: 4
0, 3, 2
added: 2
0, 4, 1
added: 1
generating min
0, 0, 0
added: 0

turnedOn = 2

hour: 0, min: 2
generating hour
0, 0, 0
added: 0
generating min
2, 0, 0
1, 1, 32
0, 2, 48
added: 48
0, 3, 40
added: 40
0, 4, 36
added: 36
0, 5, 34
added: 34
0, 6, 33
added: 33
1, 2, 16
0, 3, 24
added: 24
0, 4, 20
added: 20
0, 5, 18
added: 18
0, 6, 17
added: 17
1, 3, 8
0, 4, 12
added: 12
0, 5, 10
added: 10
0, 6, 9
added: 9
1, 4, 4
0, 5, 6
added: 6
0, 6, 5
added: 5
1, 5, 2
0, 6, 3
added: 3
1, 6, 1
hour: 1, min: 1
generating hour
1, 0, 0
0, 1, 8
added: 8
0, 2, 4
added: 4
0, 3, 2
added: 2
0, 4, 1
added: 1
generating min
1, 0, 0
0, 1, 32
added: 32
0, 2, 16
added: 16
0, 3, 8
added: 8
0, 4, 4
added: 4
0, 5, 2
added: 2
0, 6, 1
added: 1
hour: 2, min: 0
generating hour
2, 0, 0
1, 1, 8
0, 2, 12
0, 3, 10
added: 10
0, 4, 9
added: 9
1, 2, 4
0, 3, 6
added: 6
0, 4, 5
added: 5
1, 3, 2
0, 4, 3
added: 3
1, 4, 1
generating min
0, 0, 0
added: 0

LeetCode 110. Balanced Binary Tree 递归求解

   题目链接:https://leetcode.com/problems/balanced-binary-tree/ 

110. Balanced Binary Tree

My Submissions
Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems
Have you met this question in a real interview? 
Yes
 
No

Discuss


    判断一棵树是否是平衡二叉树。BST的递归定义:当一棵树的左右两棵子树的高度只差不超过1,那么该树是平衡二叉树。

    用后根遍历求解。先计算两棵子树的高度差。

    我的AC代码

public class BalancedBinaryTree {
	static boolean ok = true;
	
	public static void main(String[] args) {
		TreeNode n1 = new TreeNode(1);
		TreeNode n2 = new TreeNode(2);
		n1.left = n2;
		System.out.println(isBalanced(n1));
	}

	public static boolean isBalanced(TreeNode root) {
        ok = true;
        dfs(root);
        return ok;
    }
	
	public static int dfs(TreeNode root) {
		if(root == null || !ok) return 0;
		int ha = dfs(root.left);
		int hb = dfs(root.right);
		
		if(Math.abs(ha - hb) > 1) ok = false;
		return Math.max(ha, hb) + 1;
	}
}


以上是关于LeetCode 401. Binary Watch的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 401. Binary Watch

leetcode--401. Binary Watch

LeetCode 401. Binary Watch(二进制手表)

[LeetCode&Python] Problem 401. Binary Watch

leetcode_401_Binary Watch_回溯法_java实现

LeetCode算法题-Binary Watch(Java实现)