Skip to content

Commit ed2ab99

Browse files
committed
Add project files.
1 parent 8049857 commit ed2ab99

8 files changed

+514
-0
lines changed

LeetCode.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.106
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCodeProject", "LeetCodeProject\LeetCodeProject.csproj", "{3F46445A-F64C-4B19-9EE1-2CC2C72285A5}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3F46445A-F64C-4B19-9EE1-2CC2C72285A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3F46445A-F64C-4B19-9EE1-2CC2C72285A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3F46445A-F64C-4B19-9EE1-2CC2C72285A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3F46445A-F64C-4B19-9EE1-2CC2C72285A5}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {EBCD6144-304E-4B9A-A150-AA640722E43A}
24+
EndGlobalSection
25+
EndGlobal

LeetCodeProject/01.TwoSum.cs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace LeetCodeProject
6+
{
7+
[TestClass]
8+
public class TwoSumUnitTest
9+
{
10+
private int[] nums;
11+
private int target;
12+
[TestInitialize]
13+
public void Initialize()
14+
{
15+
this.nums = new int[4] { 2, 7, 11, 15 };
16+
this.target = 18;
17+
}
18+
19+
[TestMethod]
20+
public void TwoSumTest()
21+
{
22+
var result = this.TwoSum_LeetCodeSolution3(nums, target);
23+
24+
Assert.AreEqual(1, result[0]);
25+
Assert.AreEqual(2, result[1]);
26+
}
27+
28+
/// <summary>
29+
/// My Solution
30+
/// </summary>
31+
private int[] TwoSum_MySolution(int[] nums, int target)
32+
{
33+
for (int i = 0; i < nums.Length; i++)
34+
{
35+
for (int j = i + 1; j < nums.Length; j++)
36+
{
37+
if (nums[i] + nums[j] == target)
38+
{
39+
return new int[2] { i, j };
40+
}
41+
}
42+
}
43+
return null;
44+
}
45+
46+
/// <summary>
47+
/// LeetCode Solution 1
48+
/// </summary>
49+
private int[] TwoSum_LeetCodeSolution1(int[] nums, int target)
50+
{
51+
for (int i = 0; i < nums.Length; i++)
52+
{
53+
for (int j = i + 1; j < nums.Length; j++)
54+
{
55+
if (nums[j] == target - nums[i])
56+
{
57+
return new int[] { i, j };
58+
}
59+
}
60+
}
61+
throw new ArgumentException("No two sum solution");
62+
}
63+
64+
/// <summary>
65+
/// LeetCode Solution 2
66+
/// </summary>
67+
private int[] TwoSum_LeetCodeSolution2(int[] nums, int target)
68+
{
69+
Dictionary<int, int> map = new Dictionary<int, int>();
70+
for (int i = 0; i < nums.Length; i++)
71+
{
72+
map.Add(nums[i], i);
73+
}
74+
for (int i = 0; i < nums.Length; i++)
75+
{
76+
int complement = target - nums[i];
77+
if (map.ContainsKey(complement) && map[complement] != i)
78+
{
79+
return new int[] { i, map[complement] };
80+
}
81+
}
82+
throw new ArgumentException("No two sum solution");
83+
}
84+
85+
/// <summary>
86+
/// LeetCode Solution 3
87+
/// </summary>
88+
private int[] TwoSum_LeetCodeSolution3(int[] nums, int target)
89+
{
90+
Dictionary<int, int> map = new Dictionary<int, int>();
91+
for (int i = 0; i < nums.Length; i++)
92+
{
93+
int complement = target - nums[i];
94+
if (map.ContainsKey(complement))
95+
{
96+
return new int[] { map[complement], i };
97+
}
98+
map.Add(nums[i], i);
99+
}
100+
throw new ArgumentException("No two sum solution");
101+
}
102+
103+
}
104+
}

LeetCodeProject/02.AddTwoNumbers.cs

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace LeetCodeProject
5+
{
6+
[TestClass]
7+
public class AddTwoNumbersUnitTest
8+
{
9+
private ListNode listA;
10+
private ListNode listB;
11+
private ListNode listLongA;
12+
private ListNode listLongB;
13+
[TestInitialize]
14+
public void Initialize()
15+
{
16+
listA = new ListNode(2);
17+
listA.next = new ListNode(4);
18+
listA.next.next = new ListNode(3);
19+
listB = new ListNode(5);
20+
listB.next = new ListNode(6);
21+
listB.next.next = new ListNode(4);
22+
var arrayA = new int[61] { 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 9 };
23+
listLongA = this.ArrayToListNode(arrayA);
24+
var arrayB = new int[61] { 5, 6, 4, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 9, 9, 9, 9 };
25+
listLongB = this.ArrayToListNode(arrayB);
26+
}
27+
28+
private ListNode ArrayToListNode(int[] array)
29+
{
30+
ListNode result = null;
31+
ListNode lastNode = null;
32+
for (int i = 0; i < array.Length; i++)
33+
{
34+
var newNode = new ListNode(array[i]);
35+
if (result == null)
36+
{
37+
result = newNode;
38+
}
39+
else
40+
{
41+
lastNode.next = newNode;
42+
}
43+
lastNode = newNode;
44+
}
45+
return result;
46+
}
47+
48+
[TestMethod]
49+
public void AddTwoNumbersTest()
50+
{
51+
//var result = AddTwoNumbers_MyErrorSolution(listA, listB);
52+
//var result = AddTwoNumbers_MyErrorSolution(listLongA, listLongB);
53+
54+
var result = AddTwoNumbers_LeetCodeSolution(listLongA, listLongB);
55+
56+
Assert.AreEqual(7, result.val);
57+
Assert.AreEqual(0, result.next.val);
58+
Assert.AreEqual(8, result.next.next.val);
59+
}
60+
61+
/// <summary>
62+
/// My Solution
63+
/// </summary>
64+
private ListNode AddTwoNumbers_MySolution(ListNode l1, ListNode l2)
65+
{
66+
ListNode result = null;
67+
ListNode lastNode = null;
68+
int fact = 0;
69+
do
70+
{
71+
int v1 = l1 == null ? 0 : l1.val;
72+
int v2 = l2 == null ? 0 : l2.val;
73+
int sum = v1 + v2 + fact;
74+
int value = 0;
75+
if (sum >= 10)
76+
{
77+
fact = 1;
78+
value = sum - 10;
79+
}
80+
else
81+
{
82+
fact = 0;
83+
value = sum;
84+
}
85+
var newNode = new ListNode(value);
86+
if (result == null)
87+
{
88+
result = newNode;
89+
}
90+
else
91+
{
92+
lastNode.next = newNode;
93+
}
94+
lastNode = newNode;
95+
l1 = l1?.next;
96+
l2 = l2?.next;
97+
//参考LeetCode官方解法后发现,此判断可以移到While循环之外
98+
if (l1 == null && l2 == null && fact > 0)
99+
{
100+
lastNode.next = new ListNode(fact);
101+
//或者可以直接break掉循环了
102+
}
103+
} while (l1 != null || l2 != null);
104+
return result;
105+
}
106+
107+
/// <summary>
108+
/// My Error Solution
109+
/// 这个方法是先将ListNode转换成数字然后相加,再将结果转换成ListNode
110+
/// 这个方法的问题就是没有考虑值类型的取值范围,在一些数字超大的情况下long也不能满足
111+
/// </summary>
112+
private ListNode AddTwoNumbers_MyErrorSolution(ListNode l1, ListNode l2)
113+
{
114+
long num1 = ConvertToNumber(l1);
115+
long num2 = ConvertToNumber(l2);
116+
long sum = num1 + num2;
117+
ListNode result = null;
118+
ListNode lastNode = null;
119+
do
120+
{
121+
int value = Convert.ToInt32(sum < 10 ? sum : sum % 10);
122+
var newNode = new ListNode(value);
123+
if (result == null)
124+
{
125+
result = newNode;
126+
}
127+
else
128+
{
129+
lastNode.next = newNode;
130+
}
131+
lastNode = newNode;
132+
sum = sum / 10;
133+
134+
} while (sum > 0);
135+
return result;
136+
}
137+
138+
private long ConvertToNumber(ListNode listNode)
139+
{
140+
long result = listNode.val;
141+
long fact = 10;
142+
var next = listNode.next;
143+
while (next != null)
144+
{
145+
result = result + next.val * fact;
146+
fact = fact * 10;
147+
next = next.next;
148+
}
149+
return result;
150+
}
151+
152+
/// <summary>
153+
/// LeetCode Solution
154+
/// </summary>
155+
private ListNode AddTwoNumbers_LeetCodeSolution(ListNode l1, ListNode l2)
156+
{
157+
ListNode dummyHead = new ListNode(0);
158+
ListNode p = l1, q = l2, curr = dummyHead;
159+
int carry = 0;
160+
while (p != null || q != null)
161+
{
162+
int x = (p != null) ? p.val : 0;
163+
int y = (q != null) ? q.val : 0;
164+
int sum = carry + x + y;
165+
carry = sum / 10;
166+
curr.next = new ListNode(sum % 10);
167+
curr = curr.next;
168+
if (p != null) p = p.next;
169+
if (q != null) q = q.next;
170+
}
171+
if (carry > 0)
172+
{
173+
curr.next = new ListNode(carry);
174+
}
175+
return dummyHead.next;
176+
}
177+
}
178+
179+
180+
public class ListNode
181+
{
182+
public int val;
183+
public ListNode next;
184+
public ListNode(int x) { val = x; }
185+
}
186+
187+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace LeetCodeProject
6+
{
7+
[TestClass]
8+
public class LengthOfLongestSubstringUnitTest
9+
{
10+
11+
private string strA;
12+
private string strB;
13+
private string strC;
14+
private string strD;
15+
private string strE;
16+
[TestInitialize]
17+
public void Initialize()
18+
{
19+
this.strA = "abcabcbb";
20+
this.strB = "bbbbb";
21+
this.strC = "pwwkew";
22+
this.strD = "abcdefg";
23+
this.strE = "dvdf";
24+
}
25+
26+
[TestMethod]
27+
public void LengthOfLongestSubstringTest()
28+
{
29+
var resultA = this.LengthOfLongestSubstring_MySolutioin(strA);
30+
var resultB = this.LengthOfLongestSubstring_MySolutioin(strB);
31+
var resultC = this.LengthOfLongestSubstring_MySolutioin(strC);
32+
var resultD = this.LengthOfLongestSubstring_MySolutioin(strD);
33+
var resultE = this.LengthOfLongestSubstring_MySolutioin(strE);
34+
35+
Assert.AreEqual(3, resultA);
36+
Assert.AreEqual(1, resultB);
37+
Assert.AreEqual(3, resultC);
38+
Assert.AreEqual(7, resultD);
39+
Assert.AreEqual(3, resultE);
40+
}
41+
42+
/// <summary>
43+
/// My Solution
44+
/// </summary>
45+
private int LengthOfLongestSubstring_MySolutioin(string s)
46+
{
47+
var result = 0;
48+
var chars = s.ToCharArray();
49+
List<char> tempChars = new List<char>();
50+
for (int i = 0; i < chars.Length; i++)
51+
{
52+
if (tempChars.Contains(chars[i]))
53+
{
54+
if (tempChars.Count > result)
55+
result = tempChars.Count;
56+
var index = tempChars.IndexOf(chars[i]);
57+
tempChars.RemoveRange(0, index + 1);
58+
}
59+
tempChars.Add(chars[i]);
60+
}
61+
if (tempChars.Count > result)
62+
result = tempChars.Count;
63+
return result;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)