-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
41 lines (36 loc) · 965 Bytes
/
Solution.cs
File metadata and controls
41 lines (36 loc) · 965 Bytes
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
public class Solution
{
public string FindDifferentBinaryString(string[] nums)
{
int n = nums[0].Length;
int maxVal = (1 << n) - 1;
bool[] map = new bool[maxVal + 1];
foreach (string num in nums)
{
map[Convert.ToInt32(num, 2)] = true;
}
for (int i = 0; i <= maxVal; i++)
{
if (!map[i]) return IntToBinaryString(i, n);
}
return IntToBinaryString(maxVal, n);
}
private string IntToBinaryString(int num, int len)
{
StringBuilder sb = new();
for (int i = len - 1; i >= 0; i--)
{
sb.Append((num & (1 << i)) != 0 ? 1 : 0);
}
return sb.ToString();
}
private int BinaryStringToInt(string str)
{
int ans = 0;
for (int i = str.Length - 1; i >= 0; i--)
{
if (str[i] == '1') ans |= (1 << (str.Length - 1 - i));
}
return ans;
}
}