leetCode 401. Binary Watch

来源:互联网 发布:手机荧屏软件 编辑:程序博客网 时间:2024/05/18 08:13

1.题目

原题链接


2.算法

我们用两个嵌套循环,外循环循环小时,内循环循环分

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


0 0