Skip to content

Commit 8653801

Browse files
committedJan 26, 2020
Import C# solutions from https://github.com/kfstorm/LeetCode
1 parent 04785c8 commit 8653801

File tree

127 files changed

+5562
-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.

127 files changed

+5562
-0
lines changed
 

‎solution/0001.Two Sum/Solution.cs

+20
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 + 1, i + 1};
12+
}
13+
if (!dict.ContainsKey(nums[i]))
14+
{
15+
dict.Add(nums[i], i);
16+
}
17+
}
18+
return null;
19+
}
20+
}
+26
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.