Skip to content

Commit e7615f1

Browse files
[lEET-401] add 401
1 parent 338e3d5 commit e7615f1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FizzBuzz.java)| O(n)|O(1) | Easy|
5050
|408|[Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ValidWordAbbreviation.java)| O(n)|O(1) | Easy|
5151
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SumofLeftLeaves.java)| O(n)|O(h) | Easy|
52+
|401|[Binary Watch](https://leetcode.com/problems/binary-watch/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BinaryWatch.java)| O(1)|O(1) | Easy|
5253
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RandomPickIndex.java) | | | Medium| Reservoir Sampling
5354
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/IntegerReplacement.java)| ? | ? | Easy| BFS
5455
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/RotateFunction.java)| O(n^2) could be optimized to O(n) | O(1) | Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.stevesun;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 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).
8+
9+
Each LED represents a zero or one, with the least significant bit on the right.
10+
11+
12+
For example, the above binary watch reads "3:25".
13+
14+
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
15+
16+
Example:
17+
18+
Input: n = 1
19+
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
20+
Note:
21+
The order of output does not matter.
22+
The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
23+
The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
24+
*/
25+
public class BinaryWatch {
26+
27+
public List<String> readBinaryWatch(int num) {
28+
List<String> times = new ArrayList<>();
29+
for (int h = 0; h < 12; h++) {
30+
for (int m = 0; m < 60; m++) {
31+
if (Integer.bitCount(h*60 + m) == num) times.add(String.format("%d:%02d", h, m));//%02 means to pad this two-digit decimal number on the left with zeroes
32+
}
33+
}
34+
return times;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy