java single loop using bitCount


  • 0

    class Solution {
    public List<String> readBinaryWatch(int num) {
    List<String> ret = new ArrayList<>();
    for (int i = 0; i < 1024; i++) {
    if ((i & 768) == 768 || (i & 60) == 60) continue;
    if (Integer.bitCount(i) == num) {
    String result = Integer.toString(i >> 6);
    result += ":";
    int minutes = i & 63;
    if (minutes == 0) result += "00";
    else if (minutes < 10) result = result + "0" + minutes;
    else result = result + minutes;
    ret.add(result);
    }
    }
    return ret;
    }
    }


Log in to reply
 

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