Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9bb31e9

Browse files
Add C# solutions
1 parent 58ca119 commit 9bb31e9

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
Lines changed: 22 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 47 additions & 0 deletions
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /