-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from kfstorm/add_csharp
Import C# solutions from https://github.com/kfstorm/LeetCode
- Loading branch information
Showing
126 changed files
with
5,430 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
|
||
public class Solution { | ||
public int[] TwoSum(int[] nums, int target) { | ||
var dict = new Dictionary<int, int>(); | ||
for (var i = 0; i < nums.Length; ++i) | ||
{ | ||
int index; | ||
if (dict.TryGetValue(target - nums[i], out index)) | ||
{ | ||
return new [] { index, i}; | ||
} | ||
if (!dict.ContainsKey(nums[i])) | ||
{ | ||
dict.Add(nums[i], i); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
public class Solution { | ||
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { | ||
return AddInternal(l1, l2, false); | ||
} | ||
|
||
private ListNode AddInternal(ListNode l1, ListNode l2, bool plusOne) | ||
{ | ||
if (l1 == null && l2 == null) | ||
{ | ||
if (plusOne) | ||
{ | ||
return new ListNode(1); | ||
} | ||
return null; | ||
} | ||
|
||
var val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (plusOne ? 1 : 0); | ||
plusOne = val >= 10; | ||
val %= 10; | ||
return new ListNode(val) | ||
{ | ||
//next = AddInternal(l1?.next, l2?.next, plusOne); | ||
next = AddInternal(l1 == null ? null : l1.next, l2 == null ? null : l2.next, plusOne) | ||
}; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
solution/0003.Longest Substring Without Repeating Characters/Solution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Collections.Generic; | ||
|
||
public class Solution { | ||
public int LengthOfLongestSubstring(string s) { | ||
var hashSet = new HashSet<char>(); | ||
var maxLength = 0; | ||
int i = 0, j = 0; | ||
while (i < s.Length) | ||
{ | ||
while (hashSet.Contains(s[i])) | ||
{ | ||
hashSet.Remove(s[j++]); | ||
} | ||
hashSet.Add(s[i++]); | ||
if (i - j > maxLength) | ||
{ | ||
maxLength = i - j; | ||
} | ||
} | ||
return maxLength; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
class Range | ||
{ | ||
public static Range Empty = new Range(new int[0], 0, -1); | ||
|
||
public readonly int[] Numbers; | ||
public readonly int LeftIndex; | ||
public readonly int RightIndex; | ||
|
||
public int Count { get { return RightIndex - LeftIndex + 1; } } | ||
|
||
public int this[int index] | ||
{ | ||
get | ||
{ | ||
if (index >= Count) | ||
{ | ||
throw new IndexOutOfRangeException(); | ||
} | ||
return Numbers[LeftIndex + index]; | ||
} | ||
} | ||
|
||
public Range(int[] numbers) : this(numbers, 0, numbers.Length - 1) | ||
{ | ||
} | ||
|
||
public Range(int[] numbers, int leftIndex, int rightIndex) | ||
{ | ||
Numbers = numbers; | ||
LeftIndex = leftIndex; | ||
RightIndex = rightIndex; | ||
if (RightIndex < LeftIndex) RightIndex = LeftIndex - 1; | ||
} | ||
|
||
public Range GetSubRange(int lowerBound, int upperBound) | ||
{ | ||
if (lowerBound > upperBound) return Empty; | ||
var leftIndex = lowerBound == int.MinValue ? LeftIndex : Search(lowerBound); | ||
var rightIndex = upperBound == int.MaxValue ? RightIndex : Search(upperBound + 1) - 1; | ||
return new Range(Numbers, leftIndex, rightIndex); | ||
} | ||
|
||
private int Search(int target) | ||
{ | ||
var l = 0; | ||
var r = Numbers.Length - 1; | ||
while (l < r) | ||
{ | ||
var mid = (l + r) / 2; | ||
if (Numbers[mid] < target) | ||
{ | ||
l = mid + 1; | ||
} | ||
else | ||
{ | ||
r = mid; | ||
} | ||
} | ||
return Numbers[l] >= target ? l : l + 1; | ||
} | ||
} | ||
|
||
public class Solution { | ||
public double FindMedianSortedArrays(int[] nums1, int[] nums2) | ||
{ | ||
var totalNumbers = nums1.Length + nums2.Length; | ||
var targetOrder1 = (totalNumbers + 1)/2; | ||
var targetOrder2 = (totalNumbers + 2)/2; | ||
var range1 = new Range(nums1); | ||
var range2 = new Range(nums2); | ||
var number1 = FindMedianSortedArrays(range1, range2, targetOrder1); | ||
var number2 = targetOrder1 == targetOrder2 ? number1 : FindMedianSortedArrays(range1, range2, targetOrder2); | ||
return ((double) number1 + number2)/2; | ||
} | ||
|
||
private int FindMedianSortedArrays(Range range1, Range range2, int targetOrder) | ||
{ | ||
if (range1.Count == 0) | ||
{ | ||
return range2[targetOrder - 1]; | ||
} | ||
if (range2.Count == 0) | ||
{ | ||
return range1[targetOrder - 1]; | ||
} | ||
|
||
var midNumber = range1[(range1.Count - 1)/2]; | ||
var midRanges = new[] { range1.GetSubRange(midNumber, midNumber), range2.GetSubRange(midNumber, midNumber) }; | ||
var leftRanges = new[] | ||
{ | ||
new Range(range1.Numbers, range1.LeftIndex, midRanges[0].LeftIndex - 1), | ||
new Range(range2.Numbers, range2.LeftIndex, midRanges[1].LeftIndex - 1) | ||
}; | ||
var rightRanges = new[] | ||
{ | ||
new Range(range1.Numbers, midRanges[0].RightIndex + 1, range1.RightIndex), | ||
new Range(range2.Numbers, midRanges[1].RightIndex + 1, range2.RightIndex) | ||
}; | ||
|
||
var leftCount = leftRanges.Sum(r => r.Count); | ||
var midCount = midRanges.Sum(r => r.Count); | ||
var rightCount = rightRanges.Sum(r => r.Count); | ||
|
||
if (leftCount == 0 && rightCount == 0) | ||
{ | ||
return midNumber; | ||
} | ||
if (leftCount >= targetOrder) | ||
{ | ||
return FindMedianSortedArrays(leftRanges[0], leftRanges[1], targetOrder); | ||
} | ||
if (leftCount + midCount >= targetOrder) | ||
{ | ||
return FindMedianSortedArrays(midRanges[0], midRanges[1], targetOrder - leftCount); | ||
} | ||
return FindMedianSortedArrays(rightRanges[0], rightRanges[1], targetOrder - leftCount - midCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
public class Solution { | ||
public string LongestPalindrome(string s) { | ||
var f = new bool[s.Length]; | ||
var maxLen = 0; | ||
var index = 0; | ||
for (var p = 0; p <= 1; ++p) | ||
{ | ||
for (var l = 1 + p; l <= s.Length; l += 2) | ||
{ | ||
for (var i = 0; i <= s.Length - l; ++i) | ||
{ | ||
if (l <= 2) | ||
{ | ||
f[i] = s[i] == s[i + l - 1]; | ||
} | ||
else | ||
{ | ||
f[i] = f[i + 1] && s[i] == s[i + l - 1]; | ||
} | ||
if (f[i] && l > maxLen) | ||
{ | ||
maxLen = l; | ||
index = i; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return s.Substring(index, maxLen); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
public class Solution { | ||
public string Convert(string s, int numRows) { | ||
if (numRows == 1) return s; | ||
if (numRows > s.Length) numRows = s.Length; | ||
var rows = new List<char>[numRows]; | ||
var i = 0; | ||
var j = 0; | ||
var down = true; | ||
while (i < s.Length) | ||
{ | ||
if (rows[j] == null) | ||
{ | ||
rows[j] = new List<char>(); | ||
} | ||
rows[j].Add(s[i]); | ||
j = j + (down ? 1 : -1); | ||
if (j == numRows || j < 0) | ||
{ | ||
down = !down; | ||
j = j + (down ? 2 : -2); | ||
} | ||
++i; | ||
} | ||
return new string(rows.SelectMany(row => row).ToArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class Solution { | ||
public int Reverse(int x) { | ||
var negative = x < 0; | ||
if (negative) x = -x; | ||
long result = 0; | ||
while (x > 0) | ||
{ | ||
result = (result * 10) + x % 10; | ||
x /= 10; | ||
} | ||
if (negative) result = -result; | ||
if (result > int.MaxValue || result < int.MinValue) result = 0; | ||
return (int) result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// https://leetcode.com/problems/string-to-integer-atoi/ | ||
|
||
public partial class Solution | ||
{ | ||
public int MyAtoi(string str) | ||
{ | ||
int i = 0; | ||
long result = 0; | ||
bool minus = false; | ||
while (i < str.Length && char.IsWhiteSpace(str[i])) | ||
{ | ||
++i; | ||
} | ||
if (i < str.Length) | ||
{ | ||
if (str[i] == '+') | ||
{ | ||
++i; | ||
} | ||
else if (str[i] == '-') | ||
{ | ||
minus = true; | ||
++i; | ||
} | ||
} | ||
while (i < str.Length && char.IsDigit(str[i])) | ||
{ | ||
result = result * 10 + str[i] - '0'; | ||
if (result > int.MaxValue) | ||
{ | ||
break; | ||
} | ||
++i; | ||
} | ||
if (minus) result = -result; | ||
if (result > int.MaxValue) | ||
{ | ||
result = int.MaxValue; | ||
} | ||
if (result < int.MinValue) | ||
{ | ||
result = int.MinValue; | ||
} | ||
return (int)result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
public class Solution { | ||
public bool IsMatch(string s, string p) { | ||
var f = new bool[s.Length + 1, p.Length + 1]; | ||
f[0, 0] = true; | ||
for (var i = 0; i <= s.Length; ++i) | ||
{ | ||
for (var j = 0; j <= p.Length; ++j) | ||
{ | ||
if (i != 0 || j != 0) | ||
{ | ||
if (j == 0) | ||
{ | ||
f[i, j] = false; | ||
} | ||
else if (i == 0) | ||
{ | ||
if (p[j - 1] == '*') | ||
{ | ||
f[i, j] = f[i, j - 2]; | ||
} | ||
else | ||
{ | ||
f[i, j] = false; | ||
} | ||
} | ||
else | ||
{ | ||
if (p[j - 1] == '.') | ||
{ | ||
f[i, j] = f[i - 1, j - 1]; | ||
} | ||
else if (p[j - 1] == '*') | ||
{ | ||
f[i, j] = f[i - 1, j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.') || f[i, j - 2]; | ||
} | ||
else | ||
{ | ||
f[i, j] = f[i - 1, j - 1] && s[i - 1] == p[j - 1]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return f[s.Length, p.Length]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text; | ||
using System.Linq; | ||
|
||
public class Solution { | ||
public string LongestCommonPrefix(string[] strs) { | ||
if (strs.Length == 0) return string.Empty; | ||
var sb = new StringBuilder(); | ||
for (var i = 0; i < strs[0].Length; ++i) | ||
{ | ||
var ch = strs[0][i]; | ||
if (strs.All(str => str.Length > i && str[i] == ch)) | ||
{ | ||
sb.Append(ch); | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
} | ||
return sb.ToString(); | ||
} | ||
} |
Oops, something went wrong.