Skip to content

Commit 215774e

Browse files
committed
Add C# solutions
1 parent b6386dd commit 215774e

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Click [here](https://leetcode.com/explore/challenge/card/september-leetcoding-ch
88
Solutions in various programming languages are provided. Enjoy it.
99

1010
1. [Largest Time for Given Digits](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/01-Largest-Time-for-Given-Digits)
11+
2. [Contains Duplicate III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/02-Contains-Duplicate-III)
12+
3. [Repeated Substring Pattern](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/03-Repeated-Substring-Pattern)
13+
4. [Partition Labels](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/04-Partition-Labels)
14+
5. [All Elements in Two Binary Search Trees](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/05-All-Elements-in-Two-Binary-Search-Trees)
15+
6. [Image Overlap](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/06-Image-Overlap)
1116

1217
## August LeetCoding Challenge
1318
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t) {
3+
if (t < 0) return false;
4+
var d = new Dictionary<long,long>();
5+
long w = (long)t + 1;
6+
for (int i = 0; i < nums.Length; ++i) {
7+
long m = getID(nums[i], w);
8+
if (d.ContainsKey(m))
9+
return true;
10+
if (d.ContainsKey(m - 1) && Math.Abs(nums[i] - d[m - 1]) < w)
11+
return true;
12+
if (d.ContainsKey(m + 1) && Math.Abs(nums[i] - d[m + 1]) < w)
13+
return true;
14+
d.Add(m, (long)nums[i]);
15+
if (i >= k) d.Remove(getID(nums[i - k], w));
16+
}
17+
return false;
18+
}
19+
20+
private long getID(long i, long w) {
21+
return i < 0 ? (i + 1) / w - 1 : i / w;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public bool RepeatedSubstringPattern(string s) {
3+
var len = s.Length;
4+
for (int i = len / 2; i >= 1; i--)
5+
{
6+
// Dividable
7+
if (len % i == 0)
8+
{
9+
int m = len / i;
10+
var sub = s.Substring(0, i);
11+
int j;
12+
13+
//Check sub is repeated in each round, until m rounds.
14+
for (j = 1; j < m; j++)
15+
{
16+
if (!sub.Equals(s.Substring(j * i, i))) break;
17+
}
18+
19+
//If sub is repeated m rounds, then it's a repeated substring
20+
if (j == m)
21+
return true;
22+
}
23+
}
24+
return false;
25+
}
26+
27+
public void Test()
28+
{
29+
var s = "abcdabcdabcd";
30+
var r = RepeatedSubstringPattern(s);
31+
Console.WriteLine(r);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public IList<int> PartitionLabels(string S) {
3+
if(string.IsNullOrEmpty(S)) return null;
4+
5+
int[] largestPositionByValue = new int[26];
6+
for(int i=0; i<S.Length; i++){
7+
largestPositionByValue[S[i] - 'a'] = i;
8+
}
9+
10+
var res = new List<int>();
11+
var start = 0;
12+
var end=0;
13+
for(int i=0; i<S.Length; i++){
14+
end = Math.Max(end, largestPositionByValue[S[i]-'a']);
15+
if(end == i){
16+
res.Add(end-start+1);
17+
start = end + 1;
18+
}
19+
}
20+
return res;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Solution {
2+
public IList<int> GetAllElements(TreeNode root1, TreeNode root2) {
3+
var l1 = new List<int>();
4+
Traversal(root1, l1);
5+
var l2 = new List<int>();
6+
Traversal(root2, l2);
7+
8+
var res = Merge(l1, l2);
9+
return res;
10+
}
11+
12+
private void Traversal(TreeNode node, List<int> l){
13+
if(node == null) return;
14+
Traversal(node.left, l);
15+
l.Add(node.val);
16+
Traversal(node.right, l);
17+
}
18+
19+
private List<int> Merge(List<int> l1, List<int> l2){
20+
var res = new List<int>();
21+
int i=0, j=0;
22+
while(i<l1.Count && j<l2.Count){
23+
if(l1[i] < l2[j]){
24+
res.Add(l1[i]);
25+
i++;
26+
}else{
27+
res.Add(l2[j]);
28+
j++;
29+
}
30+
}
31+
while(i<l1.Count) {
32+
res.Add(l1[i]);
33+
i++;
34+
}
35+
while(j<l2.Count) {
36+
res.Add(l2[j]);
37+
j++;
38+
}
39+
return res;
40+
}
41+
}
42+
/**
43+
* Definition for a binary tree node.
44+
* public class TreeNode {
45+
* public int val;
46+
* public TreeNode left;
47+
* public TreeNode right;
48+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
49+
* this.val = val;
50+
* this.left = left;
51+
* this.right = right;
52+
* }
53+
* }
54+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Solution {
2+
public int LargestOverlap(int[][] A, int[][] B) {
3+
if(A == null || A.Length ==0 || B==null || B.Length==0) return 0;
4+
5+
int rows = A.Length, cols = A[0].Length;
6+
List<int[]> al = new List<int[]>(), bl = new List<int[]>();
7+
8+
for (int i = 0; i < rows; i++)
9+
{
10+
for (int j = 0; j < cols; j++)
11+
{
12+
if(A[i][j] == 1) al.Add(new int[]{i, j});
13+
if(B[i][j] == 1) bl.Add(new int[]{i, j });
14+
}
15+
}
16+
17+
var dict = new Dictionary<string, int>();
18+
foreach (int[] a in al)
19+
{
20+
foreach (int[] b in bl)
21+
{
22+
var s = (a[0] - b[0]) + " " + (a[1] - b[1]);
23+
if (dict.ContainsKey(s))
24+
{
25+
dict[s]++;
26+
}
27+
else
28+
{
29+
dict.Add(s, 1);
30+
}
31+
}
32+
}
33+
34+
int c = 0;
35+
foreach(var v in dict.Values){
36+
c = Math.Max(c, v);
37+
}
38+
return c;
39+
}
40+
}

0 commit comments

Comments
 (0)