This commit is contained in:
Electrominch
2022-10-06 00:46:00 +03:00
parent 062e03bdbf
commit cb2f99c3a8
21 changed files with 434 additions and 4 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_118._Pascal_s_Triangle</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,12 @@
namespace _118._Pascal_s_Triangle
{
internal class Program
{
static void Main(string[] args)
{
var res = new Solution().Generate(300);
foreach(var r in res)
Console.WriteLine(String.Join(" ", r));
}
}
}

View File

@@ -0,0 +1,26 @@
namespace _118._Pascal_s_Triangle;
public class Solution
{
public IList<IList<int>> Generate(int numRows)
{
List<IList<int>> triangle = new List<IList<int>>() { new List<int> { 1 } };
if(numRows > 1)
triangle.Add(new List<int>() { 1,1 });
numRows -= 2;
while (numRows-- > 0)
{
IList<int> prev = triangle[^1];
int nextCount = prev.Count;
List<int> row = new List<int>() { 1 };
for(int i = 1; i < nextCount; i++)
{
row.Add(prev[i-1] + prev[i]);
}
row.Add(1);
triangle.Add(row);
}
return triangle;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_142._Linked_List_Cycle_II</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,37 @@
namespace _142._Linked_List_Cycle_II;
//Definition for singly-linked list.
public class ListNode
{
public int val;
public ListNode? next;
public ListNode(int val = 0, ListNode? next = null)
{
this.val = val;
this.next = next;
}
public static ListNode Create(int[] nums)
{
ListNode l = new ListNode(nums[0]);
ListNode cur = l;
for (int i = 1; i < nums.Length; i++)
{
cur.next = new ListNode(nums[i]);
cur = cur.next;
}
return l;
}
public override string ToString()
{
List<int> list = new List<int>();
ListNode? cur = this;
while (cur != null)
{
list.Add(cur.val);
cur = cur.next;
}
return String.Join(" ", list);
}
}

View File

@@ -0,0 +1,15 @@
namespace _142._Linked_List_Cycle_II
{
internal class Program
{
static void Main(string[] args)
{
var l1 = new ListNode(3);
var l2 = new ListNode(2);
l1.next = l2;
l2.next = l1;
Console.WriteLine(new Solution().DetectCycle(l1)?.val);
}
}
}

View File

@@ -0,0 +1,28 @@
namespace _142._Linked_List_Cycle_II;
public class Solution
{
public ListNode? DetectCycle(ListNode head)
{
ListNode? cur = head;
int bit = 1 << 28;
int curAbs = 0;
while (cur!=null && (curAbs & bit) == 0)
{
cur.val ^= bit;
cur = cur.next;
if(cur != null)
curAbs = cur.val < 0 ? (cur.val * -1) : cur.val;
}
cur = head;
curAbs = cur.val < 0 ? (cur.val * -1) : cur.val;
while (cur != null && (curAbs & bit) != 0)
{
cur.val ^= bit;
cur = cur.next;
if (cur != null)
curAbs = cur.val < 0 ? (cur.val * -1) : cur.val;
}
return cur;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_189._Rotate_Array</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,12 @@
namespace _189._Rotate_Array
{
internal class Program
{
static void Main(string[] args)
{
var arr = new int[] { 1, 2, 3, 4, 5, 6};
new Solution().Rotate(arr, 3);
Console.WriteLine(String.Join(" ", arr));
}
}
}

View File

@@ -0,0 +1,34 @@
namespace _189._Rotate_Array;
public class Solution
{
public void Rotate(int[] nums, int k)
{
k %= nums.Length;
if (k == 0)
return;
int oneCycleLen = nums.Length/ GCD(nums.Length, k);
for (int i = 0; i < nums.Length / oneCycleLen; i++)
MoveTo(nums, nums[i], i + k, k, oneCycleLen);
}
private static void MoveTo(int[] array, int value, int to, int step, int curLen)
{
while (curLen-- > 0)
{
if (to >= array.Length)
to -= array.Length;
int buf = array[to];
array[to] = value;
to += step;
value = buf;
}
}
private static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_566._Reshape_the_Matrix</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,12 @@
namespace _566._Reshape_the_Matrix
{
internal class Program
{
static void Main(string[] args)
{
var res = new Solution().MatrixReshape(new int[][] { new int[] { 1,2 }, new int[] { 4, 5 } }, 2, 4);
foreach(var row in res)
Console.WriteLine(String.Join(" ", row));
}
}
}

View File

@@ -0,0 +1,27 @@
namespace _566._Reshape_the_Matrix;
public class Solution
{
public int[][] MatrixReshape(int[][] mat, int r, int c)
{
int[][] result = new int[r][];
for (int i = 0; i < result.Length; i++)
result[i] = new int[c];
int height = mat.Length;
int width = mat[0].Length;
int size = r * c;
if (size != height * width)
return mat;
for(int i = 0; i < size; i++)
{
int ySource = i / width;
int xSource = i % width;
int yTo = i / c;
int xTo = i % c;
result[yTo][xTo] = mat[ySource][xSource];
}
return result;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_876._Middle_of_the_Linked_List</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,37 @@
namespace _876._Middle_of_the_Linked_List;
//Definition for singly-linked list.
public class ListNode
{
public int val;
public ListNode? next;
public ListNode(int val = 0, ListNode? next = null)
{
this.val = val;
this.next = next;
}
public static ListNode Create(int[] nums)
{
ListNode l = new ListNode(nums[0]);
ListNode cur = l;
for (int i = 1; i < nums.Length; i++)
{
cur.next = new ListNode(nums[i]);
cur = cur.next;
}
return l;
}
public override string ToString()
{
List<int> list = new List<int>();
ListNode? cur = this;
while (cur != null)
{
list.Add(cur.val);
cur = cur.next;
}
return String.Join(" ", list);
}
}

View File

@@ -0,0 +1,13 @@
namespace _876._Middle_of_the_Linked_List
{
internal class Program
{
static void Main(string[] args)
{
var l1 = ListNode.Create(new int[] { 1,2,3,4,5});
var l2 = ListNode.Create(new int[] { 1,2,3,4,5,6});
Console.WriteLine(new Solution().MiddleNode(l1).val);
Console.WriteLine(new Solution().MiddleNode(l2).val);
}
}
}

View File

@@ -0,0 +1,15 @@
namespace _876._Middle_of_the_Linked_List;
public class Solution
{
public ListNode MiddleNode(ListNode? head)
{
ListNode result = head!;
while(head?.next != null)
{
head = head?.next?.next;
result = result!.next!;
}
return result;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_977._Squares_of_a_Sorted_Array</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,11 @@
namespace _977._Squares_of_a_Sorted_Array
{
internal class Program
{
static void Main(string[] args)
{
var res = new Solution().SortedSquares(new int[] { -7, -2,-1 });
Console.WriteLine(String.Join(" ", res));
}
}
}

View File

@@ -0,0 +1,49 @@
namespace _977._Squares_of_a_Sorted_Array;
public class Solution
{
public int[] SortedSquares(int[] nums)
{
int[] res = new int[nums.Length];
int firstNonNegative = -1;
for(int i = 0; i < nums.Length; i++)
{
if (nums[i] >= 0)
{
firstNonNegative = i;
break;
}
}
int resIndex = 0;
int positiveIndex = firstNonNegative;
int negativeIndex = firstNonNegative!=-1 ? positiveIndex - 1 : nums.Length-1;
while(resIndex < nums.Length)
{
if(negativeIndex>=0 && positiveIndex < nums.Length && positiveIndex>=0)
{
if (nums[negativeIndex]*-1 < nums[positiveIndex])
{
res[resIndex++] = nums[negativeIndex] * nums[negativeIndex];
negativeIndex--;
}
else
{
res[resIndex++] = nums[positiveIndex] * nums[positiveIndex];
positiveIndex++;
}
}
else if(negativeIndex >= 0)
{
res[resIndex++] = nums[negativeIndex]* nums[negativeIndex];
negativeIndex--;
}
else if(positiveIndex >= 0)
{
res[resIndex++] = nums[positiveIndex] * nums[positiveIndex];
positiveIndex++;
}
}
return res;
}
}

View File

@@ -25,13 +25,25 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "121. Best Time to Buy and S
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "21. Merge Two Sorted Lists", "21. Merge Two Sorted Lists\21. Merge Two Sorted Lists.csproj", "{3063143F-AAF9-4B23-8D98-A7236E01AA27}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "21. Merge Two Sorted Lists", "21. Merge Two Sorted Lists\21. Merge Two Sorted Lists.csproj", "{3063143F-AAF9-4B23-8D98-A7236E01AA27}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "206. Reverse Linked List", "206. Reverse Linked List\206. Reverse Linked List.csproj", "{1C1BEA62-325F-48F8-8CC0-D5828A62F14F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "206. Reverse Linked List", "206. Reverse Linked List\206. Reverse Linked List.csproj", "{1C1BEA62-325F-48F8-8CC0-D5828A62F14F}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "704. Binary Search", "704. Binary Search\704. Binary Search.csproj", "{69081812-D523-4211-AE09-9DAF8E9B83D8}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "704. Binary Search", "704. Binary Search\704. Binary Search.csproj", "{69081812-D523-4211-AE09-9DAF8E9B83D8}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "278. First Bad Version", "278. First Bad Version\278. First Bad Version.csproj", "{3BA31E13-3748-4C4A-BE68-24D2E3DF0DC1}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "278. First Bad Version", "278. First Bad Version\278. First Bad Version.csproj", "{3BA31E13-3748-4C4A-BE68-24D2E3DF0DC1}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "35. Search Insert Position", "35. Search Insert Position\35. Search Insert Position.csproj", "{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "35. Search Insert Position", "35. Search Insert Position\35. Search Insert Position.csproj", "{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "876. Middle of the Linked List", "876. Middle of the Linked List\876. Middle of the Linked List.csproj", "{2510B3B5-87EE-44AB-A2A7-BCAE9E6E91C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "566. Reshape the Matrix", "566. Reshape the Matrix\566. Reshape the Matrix.csproj", "{DD4A53CF-D840-48BF-8F8F-33E39D492BD3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "118. Pascal's Triangle", "118. Pascal's Triangle\118. Pascal's Triangle.csproj", "{E1BB66AE-E4E6-441F-9B5C-1B7FFDF7912C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "977. Squares of a Sorted Array", "977. Squares of a Sorted Array\977. Squares of a Sorted Array.csproj", "{F0B9DD20-6230-4381-8814-072C2E159624}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "189. Rotate Array", "189. Rotate Array\189. Rotate Array.csproj", "{CAF5FFD6-9598-412D-8569-EB62136D34C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "142. Linked List Cycle II", "142. Linked List Cycle II\142. Linked List Cycle II.csproj", "{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -99,6 +111,30 @@ Global
{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Debug|Any CPU.Build.0 = Debug|Any CPU {B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Release|Any CPU.ActiveCfg = Release|Any CPU {B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Release|Any CPU.Build.0 = Release|Any CPU {B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Release|Any CPU.Build.0 = Release|Any CPU
{2510B3B5-87EE-44AB-A2A7-BCAE9E6E91C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2510B3B5-87EE-44AB-A2A7-BCAE9E6E91C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2510B3B5-87EE-44AB-A2A7-BCAE9E6E91C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2510B3B5-87EE-44AB-A2A7-BCAE9E6E91C6}.Release|Any CPU.Build.0 = Release|Any CPU
{DD4A53CF-D840-48BF-8F8F-33E39D492BD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD4A53CF-D840-48BF-8F8F-33E39D492BD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD4A53CF-D840-48BF-8F8F-33E39D492BD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD4A53CF-D840-48BF-8F8F-33E39D492BD3}.Release|Any CPU.Build.0 = Release|Any CPU
{E1BB66AE-E4E6-441F-9B5C-1B7FFDF7912C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1BB66AE-E4E6-441F-9B5C-1B7FFDF7912C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1BB66AE-E4E6-441F-9B5C-1B7FFDF7912C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1BB66AE-E4E6-441F-9B5C-1B7FFDF7912C}.Release|Any CPU.Build.0 = Release|Any CPU
{F0B9DD20-6230-4381-8814-072C2E159624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0B9DD20-6230-4381-8814-072C2E159624}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0B9DD20-6230-4381-8814-072C2E159624}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0B9DD20-6230-4381-8814-072C2E159624}.Release|Any CPU.Build.0 = Release|Any CPU
{CAF5FFD6-9598-412D-8569-EB62136D34C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CAF5FFD6-9598-412D-8569-EB62136D34C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CAF5FFD6-9598-412D-8569-EB62136D34C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CAF5FFD6-9598-412D-8569-EB62136D34C8}.Release|Any CPU.Build.0 = Release|Any CPU
{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE