Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87ae406

Browse files
authored
Add files via upload
Added c# solutions for 1,49,70,217,238,242,347 and 746
1 parent 51bcb14 commit 87ae406

8 files changed

+239
-0
lines changed

csharp/1-Two-Sum.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution
2+
{
3+
public int[] TwoSum(int[] nums, int target)
4+
{
5+
var numToIndex = new Dictionary<int, int>();
6+
for (var i = 0; i < nums.Length; i++)
7+
{
8+
var diff = target - nums[i];
9+
if (numToIndex.ContainsKey(diff))
10+
{
11+
return new int[] { i, numToIndex[diff] };
12+
}
13+
else
14+
{
15+
numToIndex[nums[i]] = i;
16+
}
17+
}
18+
19+
return new int[0];
20+
}
21+
}
22+

csharp/217-Contains-Duplicate.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution
2+
{
3+
public bool ContainsDuplicate(int[] nums)
4+
{
5+
var hashSet = new HashSet<int>();
6+
foreach (var num in nums)
7+
{
8+
if (hashSet.Contains(num))
9+
{
10+
return true;
11+
}
12+
else
13+
{
14+
hashSet.Add(num);
15+
}
16+
}
17+
return false;
18+
}
19+
}

csharp/238-Product-Except-Self.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Solution5
2+
{
3+
public int[] TopKFrequent(int[] nums, int k)
4+
{
5+
var topK = new int[k];
6+
7+
var numToCount = new Dictionary<int, int>();
8+
9+
foreach (var num in nums)
10+
{
11+
if (numToCount.ContainsKey(num))
12+
{
13+
numToCount[num]++;
14+
}
15+
else
16+
{
17+
numToCount[num] = 1;
18+
}
19+
}
20+
21+
PriorityQueue<int, int> priorityQueue = new(new ElementComparer());
22+
23+
foreach (var kv in numToCount)
24+
{
25+
priorityQueue.Enqueue(kv.Key, kv.Value);
26+
}
27+
28+
for (var i = 0; i < k; i++)
29+
{
30+
topK[i] = priorityQueue.Dequeue();
31+
}
32+
33+
return topK;
34+
}
35+
}
36+
37+
public class ElementComparer : IComparer<int>
38+
{
39+
public int Compare(int x, int y)
40+
{
41+
return y - x;
42+
}
43+
}

csharp/242-Valid-Anagram.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Solution
2+
{
3+
public bool IsAnagram(string s, string t)
4+
{
5+
var sStrToCount = CreateMap(s);
6+
var tStrToCount = CreateMap(t);
7+
8+
if (sStrToCount.Keys.Count != tStrToCount.Keys.Count)
9+
{
10+
return false;
11+
}
12+
13+
foreach (var key in sStrToCount.Keys)
14+
{
15+
if (!tStrToCount.ContainsKey(key))
16+
{
17+
return false;
18+
}
19+
if (sStrToCount[key] != tStrToCount[key])
20+
{
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
27+
Dictionary<char, int> CreateMap(string str)
28+
{
29+
var dict = new Dictionary<char, int>();
30+
foreach (var ch in str)
31+
{
32+
if (dict.ContainsKey(ch))
33+
{
34+
dict[ch]++;
35+
}
36+
else
37+
{
38+
dict[ch] = 1;
39+
}
40+
}
41+
return dict;
42+
}
43+
}

csharp/347-Top-K-Frequent-Elements.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Solution
2+
{
3+
public int[] TopKFrequent(int[] nums, int k)
4+
{
5+
var topK = new int[k];
6+
7+
var numToCount = new Dictionary<int, int>();
8+
9+
foreach (var num in nums)
10+
{
11+
if (numToCount.ContainsKey(num))
12+
{
13+
numToCount[num]++;
14+
}
15+
else
16+
{
17+
numToCount[num] = 1;
18+
}
19+
}
20+
21+
PriorityQueue<int, int> priorityQueue = new(new ElementComparer());
22+
23+
foreach (var kv in numToCount)
24+
{
25+
priorityQueue.Enqueue(kv.Key, kv.Value);
26+
}
27+
28+
for (var i = 0; i < k; i++)
29+
{
30+
topK[i] = priorityQueue.Dequeue();
31+
}
32+
33+
return topK;
34+
}
35+
}
36+
37+
public class ElementComparer : IComparer<int>
38+
{
39+
public int Compare(int x, int y)
40+
{
41+
return y - x;
42+
}
43+
}

csharp/49-Group-Anagrams.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution
2+
{
3+
public IList<IList<string>> GroupAnagrams(string[] strs)
4+
{
5+
IList<IList<string>> result = new List<IList<string>>();
6+
if (strs == null || strs.Length == 0)
7+
{
8+
return result;
9+
}
10+
11+
var encodedStrToValueList = new Dictionary<string, List<string>>();
12+
foreach (var str in strs)
13+
{
14+
var arr = new int[26];
15+
foreach (var c in str)
16+
{
17+
arr[c - 'a']++;
18+
}
19+
var encodedStr = string.Join('#', arr);
20+
if (encodedStrToValueList.ContainsKey(encodedStr))
21+
{
22+
encodedStrToValueList[encodedStr].Add(str);
23+
}
24+
else
25+
{
26+
encodedStrToValueList[encodedStr] = new List<string>() { str };
27+
}
28+
}
29+
30+
foreach (var kv in encodedStrToValueList)
31+
{
32+
result.Add(kv.Value);
33+
}
34+
return result;
35+
}
36+
}

csharp/70-Climbing-Stairs.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution
2+
{
3+
public int ClimbStairs(int n)
4+
{
5+
var oneStep = 1;
6+
var twoStep = 1;
7+
8+
for (var i = 0; i < n - 1; i++)
9+
{
10+
var temp = oneStep;
11+
oneStep = oneStep + twoStep;
12+
twoStep = temp;
13+
}
14+
return oneStep;
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution
2+
{
3+
public int MinCostClimbingStairs(int[] cost)
4+
{
5+
int[] minCost = new int[cost.Length + 1];
6+
minCost[minCost.Length - 1] = 0;
7+
minCost[minCost.Length - 2] = cost[cost.Length - 1];
8+
9+
for (var i = minCost.Length - 3; i >= 0; i--)
10+
{
11+
var oneStep = cost[i] + minCost[i + 1];
12+
var twoStep = cost[i] + minCost[i + 2];
13+
minCost[i] = Math.Min(oneStep, twoStep);
14+
}
15+
return Math.Min(minCost[0], minCost[1]);
16+
}
17+
}

0 commit comments

Comments
 (0)