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 d7f977e

Browse files
Merge pull request doocs#245 from kfstorm/add_csharp
Import C# solutions from https://github.com/kfstorm/LeetCode
2 parents 04785c8 + 545e909 commit d7f977e

File tree

126 files changed

+5430
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+5430
-0
lines changed

‎solution/0001.Two Sum/Solution.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
public class Solution {
4+
public int[] TwoSum(int[] nums, int target) {
5+
var dict = new Dictionary<int, int>();
6+
for (var i = 0; i < nums.Length; ++i)
7+
{
8+
int index;
9+
if (dict.TryGetValue(target - nums[i], out index))
10+
{
11+
return new [] { index, i};
12+
}
13+
if (!dict.ContainsKey(nums[i]))
14+
{
15+
dict.Add(nums[i], i);
16+
}
17+
}
18+
return null;
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
3+
return AddInternal(l1, l2, false);
4+
}
5+
6+
private ListNode AddInternal(ListNode l1, ListNode l2, bool plusOne)
7+
{
8+
if (l1 == null && l2 == null)
9+
{
10+
if (plusOne)
11+
{
12+
return new ListNode(1);
13+
}
14+
return null;
15+
}
16+
17+
var val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (plusOne ? 1 : 0);
18+
plusOne = val >= 10;
19+
val %= 10;
20+
return new ListNode(val)
21+
{
22+
//next = AddInternal(l1?.next, l2?.next, plusOne);
23+
next = AddInternal(l1 == null ? null : l1.next, l2 == null ? null : l2.next, plusOne)
24+
};
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
3+
public class Solution {
4+
public int LengthOfLongestSubstring(string s) {
5+
var hashSet = new HashSet<char>();
6+
var maxLength = 0;
7+
int i = 0, j = 0;
8+
while (i < s.Length)
9+
{
10+
while (hashSet.Contains(s[i]))
11+
{
12+
hashSet.Remove(s[j++]);
13+
}
14+
hashSet.Add(s[i++]);
15+
if (i - j > maxLength)
16+
{
17+
maxLength = i - j;
18+
}
19+
}
20+
return maxLength;
21+
}
22+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.Linq;
3+
4+
class Range
5+
{
6+
public static Range Empty = new Range(new int[0], 0, -1);
7+
8+
public readonly int[] Numbers;
9+
public readonly int LeftIndex;
10+
public readonly int RightIndex;
11+
12+
public int Count { get { return RightIndex - LeftIndex + 1; } }
13+
14+
public int this[int index]
15+
{
16+
get
17+
{
18+
if (index >= Count)
19+
{
20+
throw new IndexOutOfRangeException();
21+
}
22+
return Numbers[LeftIndex + index];
23+
}
24+
}
25+
26+
public Range(int[] numbers) : this(numbers, 0, numbers.Length - 1)
27+
{
28+
}
29+
30+
public Range(int[] numbers, int leftIndex, int rightIndex)
31+
{
32+
Numbers = numbers;
33+
LeftIndex = leftIndex;
34+
RightIndex = rightIndex;
35+
if (RightIndex < LeftIndex) RightIndex = LeftIndex - 1;
36+
}
37+
38+
public Range GetSubRange(int lowerBound, int upperBound)
39+
{
40+
if (lowerBound > upperBound) return Empty;
41+
var leftIndex = lowerBound == int.MinValue ? LeftIndex : Search(lowerBound);
42+
var rightIndex = upperBound == int.MaxValue ? RightIndex : Search(upperBound + 1) - 1;
43+
return new Range(Numbers, leftIndex, rightIndex);
44+
}
45+
46+
private int Search(int target)
47+
{
48+
var l = 0;
49+
var r = Numbers.Length - 1;
50+
while (l < r)
51+
{
52+
var mid = (l + r) / 2;
53+
if (Numbers[mid] < target)
54+
{
55+
l = mid + 1;
56+
}
57+
else
58+
{
59+
r = mid;
60+
}
61+
}
62+
return Numbers[l] >= target ? l : l + 1;
63+
}
64+
}
65+
66+
public class Solution {
67+
public double FindMedianSortedArrays(int[] nums1, int[] nums2)
68+
{
69+
var totalNumbers = nums1.Length + nums2.Length;
70+
var targetOrder1 = (totalNumbers + 1)/2;
71+
var targetOrder2 = (totalNumbers + 2)/2;
72+
var range1 = new Range(nums1);
73+
var range2 = new Range(nums2);
74+
var number1 = FindMedianSortedArrays(range1, range2, targetOrder1);
75+
var number2 = targetOrder1 == targetOrder2 ? number1 : FindMedianSortedArrays(range1, range2, targetOrder2);
76+
return ((double) number1 + number2)/2;
77+
}
78+
79+
private int FindMedianSortedArrays(Range range1, Range range2, int targetOrder)
80+
{
81+
if (range1.Count == 0)
82+
{
83+
return range2[targetOrder - 1];
84+
}
85+
if (range2.Count == 0)
86+
{
87+
return range1[targetOrder - 1];
88+
}
89+
90+
var midNumber = range1[(range1.Count - 1)/2];
91+
var midRanges = new[] { range1.GetSubRange(midNumber, midNumber), range2.GetSubRange(midNumber, midNumber) };
92+
var leftRanges = new[]
93+
{
94+
new Range(range1.Numbers, range1.LeftIndex, midRanges[0].LeftIndex - 1),
95+
new Range(range2.Numbers, range2.LeftIndex, midRanges[1].LeftIndex - 1)
96+
};
97+
var rightRanges = new[]
98+
{
99+
new Range(range1.Numbers, midRanges[0].RightIndex + 1, range1.RightIndex),
100+
new Range(range2.Numbers, midRanges[1].RightIndex + 1, range2.RightIndex)
101+
};
102+
103+
var leftCount = leftRanges.Sum(r => r.Count);
104+
var midCount = midRanges.Sum(r => r.Count);
105+
var rightCount = rightRanges.Sum(r => r.Count);
106+
107+
if (leftCount == 0 && rightCount == 0)
108+
{
109+
return midNumber;
110+
}
111+
if (leftCount >= targetOrder)
112+
{
113+
return FindMedianSortedArrays(leftRanges[0], leftRanges[1], targetOrder);
114+
}
115+
if (leftCount + midCount >= targetOrder)
116+
{
117+
return FindMedianSortedArrays(midRanges[0], midRanges[1], targetOrder - leftCount);
118+
}
119+
return FindMedianSortedArrays(rightRanges[0], rightRanges[1], targetOrder - leftCount - midCount);
120+
}
121+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution {
2+
public string LongestPalindrome(string s) {
3+
var f = new bool[s.Length];
4+
var maxLen = 0;
5+
var index = 0;
6+
for (var p = 0; p <= 1; ++p)
7+
{
8+
for (var l = 1 + p; l <= s.Length; l += 2)
9+
{
10+
for (var i = 0; i <= s.Length - l; ++i)
11+
{
12+
if (l <= 2)
13+
{
14+
f[i] = s[i] == s[i + l - 1];
15+
}
16+
else
17+
{
18+
f[i] = f[i + 1] && s[i] == s[i + l - 1];
19+
}
20+
if (f[i] && l > maxLen)
21+
{
22+
maxLen = l;
23+
index = i;
24+
}
25+
}
26+
}
27+
}
28+
29+
return s.Substring(index, maxLen);
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
public class Solution {
5+
public string Convert(string s, int numRows) {
6+
if (numRows == 1) return s;
7+
if (numRows > s.Length) numRows = s.Length;
8+
var rows = new List<char>[numRows];
9+
var i = 0;
10+
var j = 0;
11+
var down = true;
12+
while (i < s.Length)
13+
{
14+
if (rows[j] == null)
15+
{
16+
rows[j] = new List<char>();
17+
}
18+
rows[j].Add(s[i]);
19+
j = j + (down ? 1 : -1);
20+
if (j == numRows || j < 0)
21+
{
22+
down = !down;
23+
j = j + (down ? 2 : -2);
24+
}
25+
++i;
26+
}
27+
return new string(rows.SelectMany(row => row).ToArray());
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int Reverse(int x) {
3+
var negative = x < 0;
4+
if (negative) x = -x;
5+
long result = 0;
6+
while (x > 0)
7+
{
8+
result = (result * 10) + x % 10;
9+
x /= 10;
10+
}
11+
if (negative) result = -result;
12+
if (result > int.MaxValue || result < int.MinValue) result = 0;
13+
return (int) result;
14+
}
15+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// https://leetcode.com/problems/string-to-integer-atoi/
2+
3+
public partial class Solution
4+
{
5+
public int MyAtoi(string str)
6+
{
7+
int i = 0;
8+
long result = 0;
9+
bool minus = false;
10+
while (i < str.Length && char.IsWhiteSpace(str[i]))
11+
{
12+
++i;
13+
}
14+
if (i < str.Length)
15+
{
16+
if (str[i] == '+')
17+
{
18+
++i;
19+
}
20+
else if (str[i] == '-')
21+
{
22+
minus = true;
23+
++i;
24+
}
25+
}
26+
while (i < str.Length && char.IsDigit(str[i]))
27+
{
28+
result = result * 10 + str[i] - '0';
29+
if (result > int.MaxValue)
30+
{
31+
break;
32+
}
33+
++i;
34+
}
35+
if (minus) result = -result;
36+
if (result > int.MaxValue)
37+
{
38+
result = int.MaxValue;
39+
}
40+
if (result < int.MinValue)
41+
{
42+
result = int.MinValue;
43+
}
44+
return (int)result;
45+
}
46+
}
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 IsMatch(string s, string p) {
3+
var f = new bool[s.Length + 1, p.Length + 1];
4+
f[0, 0] = true;
5+
for (var i = 0; i <= s.Length; ++i)
6+
{
7+
for (var j = 0; j <= p.Length; ++j)
8+
{
9+
if (i != 0 || j != 0)
10+
{
11+
if (j == 0)
12+
{
13+
f[i, j] = false;
14+
}
15+
else if (i == 0)
16+
{
17+
if (p[j - 1] == '*')
18+
{
19+
f[i, j] = f[i, j - 2];
20+
}
21+
else
22+
{
23+
f[i, j] = false;
24+
}
25+
}
26+
else
27+
{
28+
if (p[j - 1] == '.')
29+
{
30+
f[i, j] = f[i - 1, j - 1];
31+
}
32+
else if (p[j - 1] == '*')
33+
{
34+
f[i, j] = f[i - 1, j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.') || f[i, j - 2];
35+
}
36+
else
37+
{
38+
f[i, j] = f[i - 1, j - 1] && s[i - 1] == p[j - 1];
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
return f[s.Length, p.Length];
46+
}
47+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text;
2+
using System.Linq;
3+
4+
public class Solution {
5+
public string LongestCommonPrefix(string[] strs) {
6+
if (strs.Length == 0) return string.Empty;
7+
var sb = new StringBuilder();
8+
for (var i = 0; i < strs[0].Length; ++i)
9+
{
10+
var ch = strs[0][i];
11+
if (strs.All(str => str.Length > i && str[i] == ch))
12+
{
13+
sb.Append(ch);
14+
}
15+
else
16+
{
17+
break;
18+
}
19+
}
20+
return sb.ToString();
21+
}
22+
}

0 commit comments

Comments
(0)

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