Skip to content

Add C# solutions #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Click [here](https://leetcode.com/explore/challenge/card/september-leetcoding-ch
Solutions in various programming languages are provided. Enjoy it.

1. [Largest Time for Given Digits](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/01-Largest-Time-for-Given-Digits)
2. [Contains Duplicate III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/02-Contains-Duplicate-III)
3. [Repeated Substring Pattern](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/03-Repeated-Substring-Pattern)
4. [Partition Labels](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/04-Partition-Labels)
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)
6. [Image Overlap](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/06-Image-Overlap)

## August LeetCoding Challenge
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Solution {
public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (t < 0) return false;
var d = new Dictionary<long,long>();
long w = (long)t + 1;
for (int i = 0; i < nums.Length; ++i) {
long m = getID(nums[i], w);
if (d.ContainsKey(m))
return true;
if (d.ContainsKey(m - 1) && Math.Abs(nums[i] - d[m - 1]) < w)
return true;
if (d.ContainsKey(m + 1) && Math.Abs(nums[i] - d[m + 1]) < w)
return true;
d.Add(m, (long)nums[i]);
if (i >= k) d.Remove(getID(nums[i - k], w));
}
return false;
}

private long getID(long i, long w) {
return i < 0 ? (i + 1) / w - 1 : i / w;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Solution {
public bool RepeatedSubstringPattern(string s) {
var len = s.Length;
for (int i = len / 2; i >= 1; i--)
{
// Dividable
if (len % i == 0)
{
int m = len / i;
var sub = s.Substring(0, i);
int j;

//Check sub is repeated in each round, until m rounds.
for (j = 1; j < m; j++)
{
if (!sub.Equals(s.Substring(j * i, i))) break;
}

//If sub is repeated m rounds, then it's a repeated substring
if (j == m)
return true;
}
}
return false;
}

public void Test()
{
var s = "abcdabcdabcd";
var r = RepeatedSubstringPattern(s);
Console.WriteLine(r);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution {
public IList<int> PartitionLabels(string S) {
if(string.IsNullOrEmpty(S)) return null;

int[] largestPositionByValue = new int[26];
for(int i=0; i<S.Length; i++){
largestPositionByValue[S[i] - 'a'] = i;
}

var res = new List<int>();
var start = 0;
var end=0;
for(int i=0; i<S.Length; i++){
end = Math.Max(end, largestPositionByValue[S[i]-'a']);
if(end == i){
res.Add(end-start+1);
start = end + 1;
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
public class Solution {
public IList<int> GetAllElements(TreeNode root1, TreeNode root2) {
var l1 = new List<int>();
Traversal(root1, l1);
var l2 = new List<int>();
Traversal(root2, l2);

var res = Merge(l1, l2);
return res;
}

private void Traversal(TreeNode node, List<int> l){
if(node == null) return;
Traversal(node.left, l);
l.Add(node.val);
Traversal(node.right, l);
}

private List<int> Merge(List<int> l1, List<int> l2){
var res = new List<int>();
int i=0, j=0;
while(i<l1.Count && j<l2.Count){
if(l1[i] < l2[j]){
res.Add(l1[i]);
i++;
}else{
res.Add(l2[j]);
j++;
}
}
while(i<l1.Count) {
res.Add(l1[i]);
i++;
}
while(j<l2.Count) {
res.Add(l2[j]);
j++;
}
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
40 changes: 40 additions & 0 deletions September-LeetCoding-Challenge/06-Image-Overlap/Image-Overlap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class Solution {
public int LargestOverlap(int[][] A, int[][] B) {
if(A == null || A.Length ==0 || B==null || B.Length==0) return 0;

int rows = A.Length, cols = A[0].Length;
List<int[]> al = new List<int[]>(), bl = new List<int[]>();

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if(A[i][j] == 1) al.Add(new int[]{i, j});
if(B[i][j] == 1) bl.Add(new int[]{i, j });
}
}

var dict = new Dictionary<string, int>();
foreach (int[] a in al)
{
foreach (int[] b in bl)
{
var s = (a[0] - b[0]) + " " + (a[1] - b[1]);
if (dict.ContainsKey(s))
{
dict[s]++;
}
else
{
dict.Add(s, 1);
}
}
}

int c = 0;
foreach(var v in dict.Values){
c = Math.Max(c, v);
}
return c;
}
}