美文网首页
401. Binary Watch

401. Binary Watch

作者: hyhchaos | 来源:发表于2016-11-30 13:04 被阅读49次

感觉做起来很麻烦,参考了一下答案

Java,思路很清晰,直接读亮灯的位数

public List<String> readBinaryWatch(int num) {
    List<String> times = new ArrayList<>();
    for (int h=0; h<12; h++)
        for (int m=0; m<60; m++)
            if (Integer.bitCount(h * 64 + m) == num)
                times.add(String.format("%d:%02d", h, m));
    return times;        
}

Java,暴力

public class Solution {
        String[][] hour = {{"0"}, 
                                   {"1", "2", "4", "8"},
                   {"3", "5", "6", "9", "10"},
                   {"7", "11"}};
        String[][] minute = {{"00"}, //1
                         {"01", "02", "04", "08", "16", "32"}, //6
                         {"03", "05", "06", "09", "10", "12", "17", "18", "20", "24", "33", "34", "36", "40", "48"}, //15
                         {"07", "11", "13", "14", "19", "21", "22", "25", "26", "28", "35", "37", "38", "41", "42", "44", "49", "50", "52", "56"}, //20
                         {"15", "23", "27", "29", "30", "39", "43", "45", "46", "51", "53", "54", "57", "58"}, //14
                         {"31", "47", "55", "59"}}; //4
    public List<String> readBinaryWatch(int num) {
        List<String> ret = new ArrayList();
        for (int i = 0; i <= 3 && i <= num; i++) {
            if (num - i <= 5) {
                for (String str1 : hour[i]) {
                    for (String str2 : minute[num - i]) {
                        ret.add(str1 + ":" + str2);
                    }
                }
            }
        }
        return ret;     
    }
}

相关文章

  • 2021-05-04leetcode刷题

    401. 二进制手表[https://leetcode-cn.com/problems/binary-watch/...

  • 401. Binary Watch

    就是分钟保留两位小数,计算0-11和0-59分别把他们转成二进制以后计算其中数字1的个数之和是不是等于num,如果...

  • 401. Binary Watch

    A binary watch has 4 LEDs on the top which represent the ...

  • 401. Binary Watch

    https://leetcode.com/problems/binary-watch/discuss/88458 ...

  • 401. Binary Watch

    这题我的思路是用一个长度是10的数组,随机地把1洒落在这个这个数组上,然后对前4个做计算,后6个做计算,拼接起来就...

  • 401. Binary Watch

    感觉做起来很麻烦,参考了一下答案 Java,思路很清晰,直接读亮灯的位数 Java,暴力

  • 401. Binary Watch

    My Submissions Total Accepted: 7205Total Submissions: 167...

  • 401. Binary Watch

    A binary watch has 4 LEDs on the top which represent the ...

  • Leetcode 401. Binary Watch

    文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. S...

  • [LeetCode]401. Binary Watch

    题目 A binary watch has 4 LEDs on the top which represent t...

网友评论

      本文标题:401. Binary Watch

      本文链接:https://www.haomeiwen.com/subject/ixazpttx.html