您的位置:首页 > 其它

【LeetCode】401.Binary Watch(easy)解题报告

2018-01-14 00:09 555 查看
【LeetCode】401.Binary Watch(easy)解题报告

题目地址:https://leetcode.com/problems/binary-watch/description/

题目描述:

  A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

  Each LED represents a zero or one, with the least significant bit on the right.



  For example, the above binary watch reads “3:25”.

  Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.

Solution:

class Solution {
public List<String> readBinaryWatch(int num) {
List<String> res = new ArrayList<>();
int[] nums1 = new int[]{8,4,2,1};
int[] nums2 = new int[]{32,16,8,4,2,1};
for(int i=0;i<=num;i++){
List<Integer> list1 = generateDigit(nums1,i);
List<Integer> list2 = generateDigit(nums2,num-i);
for(int num1 : list1){
if(num1>=12) continue;
for(int num2 : list2){
if(num2>=60) continue;
res.add(num1 +":"+(num2<10?"0"+num2:num2));//多写了个s,写成了nums2
}
}
}
return res;
}
private List<Integer> generateDigit(int[] nums,int count){
List<Integer> res = new ArrayList<>();
helper(res,nums,count,0,0);
return res;
}
private void helper(List<Integer> res,int[] nums,int count,int start,int sum){
if(count==0){
res.add(sum);
return;
}
for(int i=start;i<nums.length;i++){
helper(res,nums,count-1,i+1,sum+nums[i]);
}
}
}


Date:2018年1月14日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode