Skip to content

Commit 56e1745

Browse files
authored
Merge pull request #289 from hellomrsun/master
Add C# solutions
2 parents 9f4e15c + 8b8e045 commit 56e1745

File tree

7 files changed

+139
-1
lines changed

7 files changed

+139
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public int RemoveCoveredIntervals(int[][] intervals) {
3+
Array.Sort(intervals, (x, y) => x[0]==y[0] ? y[1]-x[1] : x[0]-y[0]);
4+
int count = 0;
5+
int curr = 0;
6+
foreach(int[] interval in intervals){
7+
if(curr < interval[1]){
8+
curr = interval[1];
9+
count++;
10+
}
11+
}
12+
return count;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public bool BuddyStrings(string A, string B) {
3+
if((A == null && B != null) || (A != null && B == null)) return false;
4+
if(A.Length != B.Length) return false;
5+
int diffCount = 0;
6+
int a = -1, b = -1;
7+
bool canSwitch = false;
8+
int[] count = new int[26];
9+
10+
for(int i=0; i<A.Length; i++){
11+
if(++count[A[i] - 'a'] >= 2) canSwitch = true;
12+
13+
if(A[i] != B[i])
14+
{
15+
diffCount++;
16+
if (a == -1) a = i;
17+
else if (b == -1) b = i;
18+
}
19+
}
20+
return (diffCount == 0 && canSwitch) || (diffCount == 2 && A[a] == B[b] && A[b] == B[a]);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int Rob(int[] nums) {
3+
if(nums == null || nums.Length == 0) return 0;
4+
if(nums.Length == 1) return nums[0];
5+
6+
return Math.Max(rob(nums, 1, nums.Length-1), rob(nums, 0, nums.Length-2));
7+
}
8+
9+
private int rob(int[] nums, int lo, int hi){
10+
int include = 0, exclude = 0;
11+
for(int x=lo; x<=hi; x++){
12+
int i=include, e=exclude;
13+
include = e + nums[x];
14+
exclude = Math.Max(i, e);
15+
}
16+
return Math.Max(include, exclude);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public void Rotate(int[] nums, int k) {
3+
if(nums == null || nums.Length < 2) return;
4+
var length = nums.Length;
5+
int n = k % length;
6+
//Reverse left part
7+
Reverse(nums, 0, length - 1 - n);
8+
//Reverse right part
9+
Reverse(nums, length - n, length-1);
10+
//Reverse whole array
11+
Reverse(nums, 0, length-1);
12+
}
13+
private void Reverse(int[] nums, int b, int e){
14+
int tmp = 0;
15+
while(b < e){
16+
tmp = nums[b];
17+
nums[b] = nums[e];
18+
nums[e] = tmp;
19+
b++;
20+
e--;
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Solution {
2+
public bool SearchMatrix(int[][] matrix, int target)
3+
{
4+
if (matrix == null || matrix.Length == 0) return false;
5+
var levelLen = matrix[0].Length;
6+
if(levelLen == 0) return false;
7+
8+
bool result = false;
9+
for (int i = 0; i <= matrix.Length-1; i++)
10+
{
11+
if (target <= matrix[i][levelLen - 1])
12+
{
13+
return BinarySearch(target, matrix[i]);
14+
}
15+
}
16+
17+
return false;
18+
}
19+
20+
private bool BinarySearch(int target, int[] nums)
21+
{
22+
int lo = 0, hi = nums.Length - 1;
23+
int mid = 0;
24+
while (lo <= hi)
25+
{
26+
mid = lo + (hi - lo) / 2;
27+
28+
if (nums[mid] > target)
29+
{
30+
hi = mid-1;
31+
}
32+
else if (nums[mid] < target)
33+
{
34+
lo = mid+1;
35+
}
36+
else if (nums[mid] == target)
37+
{
38+
return true;
39+
}
40+
else
41+
{
42+
return false;
43+
}
44+
}
45+
return false;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public IList<string> FindRepeatedDnaSequences(string s) {
3+
if(s == null || s.Length == 0) return new List<string>();
4+
var seen = new HashSet<string>();
5+
var repeated = new HashSet<string>();
6+
for(int i=0; i<s.Length-9; i++){
7+
string curr = s.Substring(i, 10);
8+
if(!seen.Add(curr)){
9+
repeated.Add(curr);
10+
}
11+
}
12+
return new List<string>(repeated);
13+
}
14+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Click [here](https://leetcode.com/explore/challenge/card/october-leetcoding-chal
99
Solutions in various programming languages are provided. Enjoy it.
1010

1111
1. [Number of Recent Calls](https://github.com/AlgoStudyGroup/Leetcode/tree/master/October-LeetCoding-Challenge/01-Number-of-Recent-Calls) : Queue
12-
12+
4. [Remove Covered Intervals](https://github.com/AlgoStudyGroup/Leetcode/tree/master/October-LeetCoding-Challenge/04-Remove-Covered-Intervals)
1313

1414
## September LeetCoding Challenge
1515
Click [here](https://leetcode.com/explore/challenge/card/september-leetcoding-challenge) for problem descriptions.

0 commit comments

Comments
 (0)