-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
43 lines (36 loc) · 1 KB
/
Solution.cs
File metadata and controls
43 lines (36 loc) · 1 KB
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
namespace LeetCode.Problem268{
//268. Missing Number
//https://leetcode.com/problems/missing-number/
/*
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
*/
public class Solution {
public int MissingNumber(int[] nums) {
Dictionary<int,int> values = new Dictionary<int,int>();
for (int i = 0; i < nums.Length; i++)
{
var value = nums[i];
values.Add(value,i);
}
int ans = 0;
while (ans <= values.Count)
{
if (!values.ContainsKey(ans))
return ans;
ans++;
}
return -1;
}
}
public class Alternative{
public int MissingNumber(int[] nums) {
HashSet<int> set = new HashSet<int>(nums);
for (int i = 0; i <= set.Count; i++)
{
if (!set.Contains(i))
return i;
}
return -1;
}
}
}