Skip to content

Commit 60fb7a5

Browse files
committed
feat(solution): Added C# solutions for Two Sum, Contains Duplicate, and Group Anagrams
1 parent bd81a12 commit 60fb7a5

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

C#/1-Two_Sum.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// URL: https://leetcode.com/problems/two-sum/
2+
// Time Complexity: O(n)
3+
// Space Complexity: O(n)
4+
5+
public class Solution
6+
{
7+
public int[] TwoSum(int[] nums, int target)
8+
{
9+
Dictionary<int, int> map = new Dictionary<int, int>();
10+
11+
for (int i = 0; i < nums.Length; i++)
12+
{
13+
int diff = target - nums[i];
14+
15+
if (map.ContainsKey(diff))
16+
{
17+
return new int[] { i, map[diff] };
18+
}
19+
20+
map[nums[i]] = i;
21+
}
22+
23+
return null;
24+
}
25+
}

C#/217-Contains_Duplicate.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// URL: https://leetcode.com/problems/contains-duplicate/
2+
// Time Complexity: O(n)
3+
// Space Complexity: O(n)
4+
5+
public class Solution
6+
{
7+
public bool ContainsDuplicate(int[] nums)
8+
{
9+
return nums.Length != new HashSet<int>(nums).Count;
10+
}
11+
}

C#/49-Group_Anagrams.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// URL: https://leetcode.com/problems/group-anagrams/
2+
// Time Complexity: O(n * m * log(m))
3+
// Space Complexity: O(n * m)
4+
5+
public class Solution
6+
{
7+
public IList<IList<string>> GroupAnagrams(string[] strs)
8+
{
9+
Dictionary<string, IList<string>> dict = new Dictionary<string, IList<string>>();
10+
11+
for (int i = 0; i < strs.Length; i++)
12+
{
13+
char[] arr = strs[i].ToCharArray();
14+
Array.Sort(arr);
15+
String orderedString = new String(arr);
16+
17+
if (!dict.ContainsKey(orderedString))
18+
{
19+
dict.Add(orderedString, new List<String>() { strs[i] });
20+
}
21+
else
22+
{
23+
dict[orderedString].Add(strs[i]);
24+
}
25+
}
26+
27+
return dict.Values.ToList();
28+
}
29+
}

0 commit comments

Comments
 (0)