Skip to content

Commit ace8574

Browse files
committed
added some new solved problems
1 parent 8c5e128 commit ace8574

File tree

14 files changed

+526
-0
lines changed

14 files changed

+526
-0
lines changed

LeetCode.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddTwoNumbers", "AddTwoNumb
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LongestSubstringWithoutRepeating", "LongestSubstringWithoutRepeating\LongestSubstringWithoutRepeating.csproj", "{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RomanToInt", "RomanToInt\RomanToInt.csproj", "{8F1541CA-654D-44F6-8615-2B81566CAE44}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetcodeTasks", "LeetcodeTasks\LeetcodeTasks.csproj", "{074BA7FF-86C3-4BDC-981F-7A715A2A6B1F}"
15+
EndProject
1216
Global
1317
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1418
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +31,14 @@ Global
2731
{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
2832
{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
2933
{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{8F1541CA-654D-44F6-8615-2B81566CAE44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{8F1541CA-654D-44F6-8615-2B81566CAE44}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{8F1541CA-654D-44F6-8615-2B81566CAE44}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{8F1541CA-654D-44F6-8615-2B81566CAE44}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{074BA7FF-86C3-4BDC-981F-7A715A2A6B1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{074BA7FF-86C3-4BDC-981F-7A715A2A6B1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{074BA7FF-86C3-4BDC-981F-7A715A2A6B1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{074BA7FF-86C3-4BDC-981F-7A715A2A6B1F}.Release|Any CPU.Build.0 = Release|Any CPU
3042
EndGlobalSection
3143
GlobalSection(SolutionProperties) = preSolution
3244
HideSolutionNode = FALSE

LeetcodeTasks/LeetcodeTasks.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="nunit" Version="3.12.0" />
11+
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace LeetcodeTasks.MergeTwoSortedLinkedLists
2+
{
3+
public class ListNode
4+
{
5+
public int val;
6+
public ListNode next;
7+
public ListNode(int val=0, ListNode next=null)
8+
{
9+
this.val = val;
10+
this.next = next;
11+
}
12+
}
13+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
namespace LeetcodeTasks.MergeTwoSortedLinkedLists
2+
{
3+
public class MergeTwoSortedLinkedList
4+
{
5+
private readonly ListNode _first;
6+
private readonly ListNode _second;
7+
8+
public MergeTwoSortedLinkedList(ListNode first, ListNode second)
9+
{
10+
_first = first;
11+
_second = second;
12+
}
13+
14+
public ListNode SolutionOne()
15+
{
16+
var left = _first;
17+
var right = _second;
18+
ListNode start = null;
19+
ListNode current = null;
20+
21+
while (left != null && right != null)
22+
{
23+
if (left.val <= right.val)
24+
{
25+
if (start == null)
26+
{
27+
current = start = left;
28+
}
29+
else
30+
{
31+
current = current.next = left;
32+
}
33+
left = left.next;
34+
}
35+
else
36+
{
37+
if (start == null)
38+
{
39+
current = start = right;
40+
}
41+
else
42+
{
43+
current = current.next = right;
44+
}
45+
right = right.next;
46+
}
47+
}
48+
49+
if (left != null)
50+
while (left != null)
51+
{
52+
if (start == null)
53+
{
54+
current = start = left;
55+
}
56+
else
57+
{
58+
current = current.next = left;
59+
}
60+
left = left.next;
61+
}
62+
63+
if (right != null)
64+
while (right != null)
65+
{
66+
if (start == null)
67+
{
68+
current = start = right;
69+
}
70+
else
71+
{
72+
current = current.next = right;
73+
}
74+
right = right.next;
75+
}
76+
77+
return start;
78+
}
79+
80+
}
81+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using NUnit.Framework;
2+
3+
namespace LeetcodeTasks.MergeTwoSortedLinkedLists
4+
{
5+
[TestFixture]
6+
public class MergeTwoSortedLinkedListTest
7+
{
8+
[Test]
9+
public void TwoSortedListWithSameCountButDifferentNumbers_ShouldSucceeded()
10+
{
11+
var l1 = new ListNode(1,new ListNode(3,new ListNode(5)));
12+
var l2= new ListNode(2,new ListNode(4,new ListNode(6)));
13+
14+
var solution = new Solution();
15+
var result = solution.MergeTwoLists(l1, l2);
16+
17+
Assert.NotNull(result);
18+
}
19+
20+
[Test]
21+
public void TwoSortedListWithDiffCountAndDiffNumbers_ShouldSucceeded()
22+
{
23+
var l1 = new ListNode(1,new ListNode(1,new ListNode(5, new ListNode(10))));
24+
var l2= new ListNode(2,new ListNode(4,new ListNode(6, new ListNode(9, new ListNode(20, new ListNode(21))))));
25+
26+
var solution = new Solution();
27+
var result = solution.MergeTwoLists(l1, l2);
28+
29+
Assert.NotNull(result);
30+
}
31+
32+
[Test]
33+
public void TwoSortedList_FirstNull_SecondWithOneElement_ShouldSucceeded()
34+
{
35+
ListNode l1 = null;
36+
var l2= new ListNode(0);
37+
38+
var solution = new Solution();
39+
var result = solution.MergeTwoLists(l1, l2);
40+
41+
Assert.NotNull(result);
42+
}
43+
44+
45+
46+
}
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace LeetcodeTasks.MergeTwoSortedLinkedLists
2+
{
3+
public class Solution
4+
{
5+
public ListNode MergeTwoLists(ListNode l1, ListNode l2)
6+
{
7+
var mergeTwoSorterLinkedList = new MergeTwoSortedLinkedList(l1, l2);
8+
return mergeTwoSorterLinkedList.SolutionOne();
9+
}
10+
}
11+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using NUnit.Framework;
2+
3+
namespace LeetcodeTasks.RemoveDublicatesArray
4+
{
5+
public class RemoveDublicatesInArrayTest
6+
{
7+
[Test]
8+
public void ArrayWithTwoRepeatedNumbers()
9+
{
10+
var arr = new int[] {1, 1, 2, 2};
11+
var sol = new Solution();
12+
13+
Assert.AreEqual(2,sol.RemoveDuplicates(arr));
14+
}
15+
16+
[Test]
17+
public void ArrayWithTwoRepeatedAndOneNumbers()
18+
{
19+
var arr = new int[] {1, 1, 2, 2,10};
20+
var sol = new Solution();
21+
22+
Assert.AreEqual(3,sol.RemoveDuplicates(arr));
23+
}
24+
25+
[Test]
26+
public void ArrayWithAllSameNumbers()
27+
{
28+
var arr = new int[] {1, 1, 1, 1,1};
29+
var sol = new Solution();
30+
31+
Assert.AreEqual(1,sol.RemoveDuplicates(arr));
32+
}
33+
34+
[Test]
35+
public void ArrayWithNegativeAndPositiveNumbers()
36+
{
37+
var arr = new int[] {-1,0,0, 1, 1, 3,6,7,7};
38+
var sol = new Solution();
39+
40+
Assert.AreEqual(6,sol.RemoveDuplicates(arr));
41+
}
42+
43+
[Test]
44+
public void EmtyArray()
45+
{
46+
var arr = new int[]{};
47+
var sol = new Solution();
48+
49+
Assert.AreEqual(0,sol.RemoveDuplicates(arr));
50+
}
51+
52+
53+
}
54+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
namespace LeetcodeTasks.RemoveDublicatesArray
4+
{
5+
public class RemoveDublicatesIsArray
6+
{
7+
private int[] _array;
8+
9+
public RemoveDublicatesIsArray(int[] arr)
10+
{
11+
_array = arr;
12+
}
13+
14+
public int SolutionOne()
15+
{
16+
if (_array == null || _array.Length == 0)
17+
{
18+
return 0;
19+
}
20+
21+
if (_array.Length == 1)
22+
{
23+
return 1;
24+
}
25+
26+
var i = 0;
27+
var j = 0;
28+
while (j < _array.Length)
29+
{
30+
if (_array[i] != _array[j])
31+
{
32+
i++;
33+
_array[i] = _array[j];
34+
}
35+
j++;
36+
}
37+
return i + 1;
38+
}
39+
}
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace LeetcodeTasks.RemoveDublicatesArray
2+
{
3+
public class Solution
4+
{
5+
public int RemoveDuplicates(int[] nums)
6+
{
7+
var removeDub = new RemoveDublicatesIsArray(nums);
8+
return removeDub.SolutionOne();
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace LeetcodeTasks.ValidParentheses
2+
{
3+
public class Solution
4+
{
5+
public bool IsValid(string s)
6+
{
7+
var validParentheses = new ValidParentheses(s);
8+
return validParentheses.SolutionOne();
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)