-
Notifications
You must be signed in to change notification settings - Fork 112
/
0401-BinaryWatch.cs
41 lines (34 loc) · 1016 Bytes
/
0401-BinaryWatch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//-----------------------------------------------------------------------------
// Runtime: 236ms
// Memory Usage: 31.1 MB
// Link: https://leetcode.com/submissions/detail/351930109/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0401_BinaryWatch
{
public IList<string> ReadBinaryWatch(int num)
{
var results = new List<string>();
for (int i = 0; i < 720; i++)
{
int hour = i / 60;
int minute = i % 60;
if (BitCount(hour) + BitCount(minute) == num)
results.Add($"{hour}:{minute:00}");
}
return results;
}
private static int BitCount(int num)
{
var result = 0;
while (num > 0)
{
result++;
num &= num - 1;
}
return result;
}
}
}