【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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201801/ded075f4b857b3760c0961983b1ed623)
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:
Date:2018年1月14日
题目地址: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解题报告 1. Two Sum[easy]
- 【LeetCode】447.Number of Boomerangs(Easy)解题报告
- LeetCode解题报告 111. Minimum Depth of Binary Tree [easy]
- 【LeetCode】243.Shortest Word Distance(Easy)解题报告
- 【LeetCode】206.Reverse Linked List(Easy)解题报告
- 【LeetCode】217.Contains Duplicate(Easy)解题报告
- 【LeetCode】455.Assign Cookies(Easy)解题报告
- LeetCode解题报告 383. Ransom Note [easy]
- 【LeetCode】234.Palindrome Linked List(Easy)解题报告
- 【LeetCode】27.Remove Element(Easy)解题报告
- 【LeetCode】27.Remove Element(Easy)解题报告
- 【LeetCode】118.Pascal's Triangle(easy)解题报告
- 【LeetCode】687.Longest Univalue Path(Easy)解题报告
- LeetCode解题报告 70. Climbing Stairs [easy]
- 【LeetCode】26.Remove Duplicates from Sorted Array(Easy)解题报告
- 【LeetCode】53.Maximum Subarray(Easy)解题报告
- 【LeetCode】581.Shortest Unsorted Continuous Subarray(easy)解题报告
- LeetCode解题报告 100. Same Tree [easy]
- 【LeetCode】235.Lowest Common Ancestor of a Binary Search Tree(Easy)解题报告
- 【LeetCode】100.Same Tree(Easy)解题报告