Just for fun!!!!!!! java 1ms, beats 100%


  • 62
    C
    public class Solution {
        String[][] hour = {{"0"},
                {"1", "2", "4", "8"},
                {"3", "5", "6", "9", "10"},
                {"7", "11"}};
        String[][] minute = {{"00"},
                {"01", "02", "04", "08", "16", "32"},
                {"03", "05", "06", "09", "10", "12", "17", "18", "20", "24", "33", "34", "36", "40", "48"},
                {"07", "11", "13", "14", "19", "21", "22", "25", "26", "28", "35", "37", "38", "41", "42", "44", "49", "50", "52", "56"},
                {"15", "23", "27", "29", "30", "39", "43", "45", "46", "51", "53", "54", "57", "58"},
                {"31", "47", "55", "59"}};
    
        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;
        }
    }
    

  • 72

    when see your code, and then I want to said MDZZ to myself


  • 0
    Z

  • 0
    L

    @zhangCabbage hahaha


  • 0
    S

    interesting answer


  • 0
    S

  • 0
    P

    @zhangCabbage 666666666666666


  • 8
    C

    In industry, this is the real answer~
    DFS? Just say "fck off"! Only interviewers like it...


  • 0
    H

    66666666
    6 de fei qi


  • 0
    W

    That's why it is EASY !


  • 17

    What a nice solution!! I added couple of comments below for further explanation

    public class Solution {
    		String[][] hour = {{"0"},  // hours contains 0 1's
    				   {"1", "2", "4", "8"},   // hours contains 1 1's
    				   {"3", "5", "6", "9", "10"},  // hours contains 2 1's
    				   {"7", "11"}};  // hours contains 3 1's
    		String[][] minute = {{"00"},  // mins contains 0 1's
    			             {"01", "02", "04", "08", "16", "32"},  // mins contains 1 1's
    			             {"03", "05", "06", "09", "10", "12", "17", "18", "20", "24", "33", "34", "36", "40", "48"},  // mins contains 2 1's
    			             {"07", "11", "13", "14", "19", "21", "22", "25", "26", "28", "35", "37", "38", "41", "42", "44", "49", "50", "52", "56"},  // mins contains 3 1's
    			             {"15", "23", "27", "29", "30", "39", "43", "45", "46", "51", "53", "54", "57", "58"},  // mins contains 4 1's
    			             {"31", "47", "55", "59"}};  // mins contains 5 1's
        public List<String> readBinaryWatch(int num) {
    		List<String> ret = new ArrayList();
                   // loop from 0 to 3 which is the max number of bits can be set in hours (4 bits)
    		for (int i = 0; i <= 3 && i <= n; i++) {
                            // this if condition is to make sure the index from minutes array would be valid
    			if (n - i <= 5) {
                                   // if we have i 1's in hours, then we need n - i 1's in minutes, that's why the arrays were created by grouping the number of 1's bits
    				for (String str1 : hour[i]) {
    					for (String str2 : minute[n - i]) {
    						ret.add(str1 + ":" + str2);
    					}
    				}
    			}
    		}
    		return ret;     
        }
    }
    

  • 0
    D

    This alg puts the responsibility of computing to programmer's brain or another alg.


  • 0
    N

    This is really just for fun.

    As if you include on the time to calculate the "values of hour & minute", it would be more than 1ms.

    But in reality, I think solution is very good for the special business requirement. LOL


  • 0
    M

    I think the "n" in your code should actually be "num".


  • 0
    M

  • 0
    V

    LOL nice 1ms time -.-


  • 0
    L

    @cuberyao 66666666666666666666666


  • 0
    C

    C++ including the computing time, 3ms :)

    class Solution {
        unordered_map<int, vector<int> > hours;
        unordered_map<int, vector<int> > minutes;
    public:
        vector<string> readBinaryWatch(int num) {
            init();
            vector<string> res;
            if(num > 10) return res;
            for(int i = 0; i < 4; i++){
                int j = num - i;
                if(j > 5 || j < 0) continue;
                for(int k: hours[i]){
                    for(int h: minutes[j]){
                        string b = to_string(h);
                        if(b.length() <= 1){
                            b = "0" + b;
                        }
                        res.push_back(to_string(k) + ":" + b);
                    }
                }
            }
            return res;
        }
        
        int compute(int i){
            int res = 0;
            while(i != 0){
                res++;
                i &= i - 1;
            }
            return res;
        }
        
        void init(){
            for(int i = 0; i < 12; i++){
                int j = compute(i);
                if(hours.find(j) == hours.end()){
                    hours[j] = vector<int>();
                }
                hours[j].push_back(i);
            }
            for(int i = 0; i < 60; i++){
                int j = compute(i);
                if(minutes.find(j) == minutes.end()){
                    minutes[j] = vector<int>();
                }
                minutes[j].push_back(i);
            }
        }
    };
    

  • 0
    S

    haha it certainly beats 100% ;)


  • 0

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.