-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
58 lines (53 loc) · 1.39 KB
/
Program.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
namespace DesignPhoneDirectory
{
class Program
{
static void Main(string[] args)
{
var phone = new PhoneDirectory(2);
phone.Release(1);
Console.WriteLine(phone.Get());
Console.WriteLine(phone.Check(1));
Console.WriteLine(phone.Check(1));
phone.Release(1);
Console.WriteLine(phone.Check(1));
Console.WriteLine(phone.Get());
Console.WriteLine(phone.Check(0));
Console.WriteLine(phone.Check(1));
Console.WriteLine(phone.Check(1));
}
}
public class PhoneDirectory
{
private int[] record;
public PhoneDirectory(int maxNumbers)
{
record = new int[maxNumbers];
for (int i = 0; i < maxNumbers; i++)
record[i]++;
}
public int Get()
{
for (int i = 0; i < record.Length; i++)
{
if (record[i] > 0)
{
record[i]--;
return i;
}
}
return -1;
}
public bool Check(int number)
{
return record[number] > 0;
}
public void Release(int number)
{
if (record[number] == 0)
record[number]++;
}
}
}