Day8-9
This commit is contained in:
11
20. Valid Parentheses/20. Valid Parentheses.csproj
Normal file
11
20. Valid Parentheses/20. Valid Parentheses.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_20._Valid_Parentheses</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
9
20. Valid Parentheses/Program.cs
Normal file
9
20. Valid Parentheses/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace _20._Valid_Parentheses;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine(new Solution().IsValid("([)]"));
|
||||||
|
}
|
||||||
|
}
|
||||||
68
20. Valid Parentheses/Solution.cs
Normal file
68
20. Valid Parentheses/Solution.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace _20._Valid_Parentheses;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public bool IsValid(string s)
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder(s);
|
||||||
|
if (sb.Length == 0)
|
||||||
|
return true;
|
||||||
|
if (sb.Length % 2 == 1)
|
||||||
|
return false;
|
||||||
|
while(sb.Length > 0)
|
||||||
|
{
|
||||||
|
char ch = sb[0];
|
||||||
|
if ("({[".Contains(ch) == false)
|
||||||
|
return false;
|
||||||
|
char need = ch switch
|
||||||
|
{
|
||||||
|
'(' => ')',
|
||||||
|
'{' => '}',
|
||||||
|
'[' => ']',
|
||||||
|
_ => '\0'
|
||||||
|
};
|
||||||
|
int count1 = 0;// ()
|
||||||
|
int count2 = 0;// {}
|
||||||
|
int count3 = 0;// []
|
||||||
|
bool succes = false;
|
||||||
|
for(int i = 1; i < sb.Length; i++)
|
||||||
|
{
|
||||||
|
char c = sb[i];
|
||||||
|
if (count1 == 0 && count2 == 0 && count3 == 0 && c == need)
|
||||||
|
{
|
||||||
|
sb.Remove(i, 1).Remove(0, 1);
|
||||||
|
succes = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
switch (c)
|
||||||
|
{
|
||||||
|
case '(':
|
||||||
|
count1++;
|
||||||
|
break;
|
||||||
|
case ')':
|
||||||
|
count1--;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '{':
|
||||||
|
count2++;
|
||||||
|
break;
|
||||||
|
case '}':
|
||||||
|
count2--;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '[':
|
||||||
|
count3++;
|
||||||
|
break;
|
||||||
|
case ']':
|
||||||
|
count3--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(succes == false)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
200. Number of Islands/200. Number of Islands.csproj
Normal file
11
200. Number of Islands/200. Number of Islands.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_200._Number_of_Islands</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
9
200. Number of Islands/Program.cs
Normal file
9
200. Number of Islands/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace _200._Number_of_Islands;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
42
200. Number of Islands/Solution.cs
Normal file
42
200. Number of Islands/Solution.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
|
namespace _200._Number_of_Islands;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int NumIslands(char[][] grid)
|
||||||
|
{
|
||||||
|
int[][] was = new int[grid.Length][];
|
||||||
|
for (int i = 0; i < grid.Length; i++)
|
||||||
|
was[i] = new int[grid[i].Length];
|
||||||
|
|
||||||
|
int curId = 0;
|
||||||
|
for (int y = 0; y < grid.Length; y++)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < grid[y].Length; x++)
|
||||||
|
{
|
||||||
|
if (was[y][x] == 0 && grid[y][x] == '1')
|
||||||
|
curId++;
|
||||||
|
else
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Recursion(grid, was, y, x, curId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return curId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Recursion(char[][] grid, int[][] was, int y, int x, int id)
|
||||||
|
{
|
||||||
|
if (was[y][x]!=0 || grid[y][x] != '1')
|
||||||
|
return;
|
||||||
|
was[y][x] = id;
|
||||||
|
for (int i = 1; i < 8; i += 2)
|
||||||
|
{
|
||||||
|
int nextY = i / 3 + y - 1;
|
||||||
|
int nextX = i % 3 + x - 1;
|
||||||
|
if (nextY >= 0 && nextY < was.Length && nextX >= 0 && nextX < was[nextY].Length)
|
||||||
|
Recursion(grid, was, nextY, nextX, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_232._Implement_Queue_using_Stacks</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
35
232. Implement Queue using Stacks/MyQueue.cs
Normal file
35
232. Implement Queue using Stacks/MyQueue.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
namespace _232._Implement_Queue_using_Stacks;
|
||||||
|
|
||||||
|
public class MyQueue
|
||||||
|
{
|
||||||
|
private Stack<int> _sInput = new Stack<int>();
|
||||||
|
private Stack<int> _sOutput = new Stack<int>();
|
||||||
|
|
||||||
|
public void Push(int x)
|
||||||
|
{
|
||||||
|
while(_sOutput.TryPop(out int res))
|
||||||
|
_sInput.Push(res);
|
||||||
|
_sInput.Push(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Pop()
|
||||||
|
{
|
||||||
|
while(_sInput.TryPop(out int res))
|
||||||
|
_sOutput.Push(res);
|
||||||
|
return _sOutput.Pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Peek()
|
||||||
|
{
|
||||||
|
while (_sInput.TryPop(out int res))
|
||||||
|
_sOutput.Push(res);
|
||||||
|
return _sOutput.Peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Empty()
|
||||||
|
{
|
||||||
|
while (_sInput.TryPop(out int res))
|
||||||
|
_sOutput.Push(res);
|
||||||
|
return _sOutput.Count == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
232. Implement Queue using Stacks/Program.cs
Normal file
9
232. Implement Queue using Stacks/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace _232._Implement_Queue_using_Stacks;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_235._Lowest_Common_Ancestor_of_a_Binary_Search_Tree</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace _235._Lowest_Common_Ancestor_of_a_Binary_Search_Tree;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
TreeNode root = new TreeNode(6);
|
||||||
|
root.left = new TreeNode(2) { left = new TreeNode(0), right = new TreeNode(4) { left = new TreeNode(3), right = new TreeNode(5) } };
|
||||||
|
root.right = new TreeNode(8) { left = new TreeNode(7), right = new TreeNode(9) };
|
||||||
|
Console.WriteLine(new Solution().LowestCommonAncestor(root, new TreeNode(3), new TreeNode(5)).val);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
namespace _235._Lowest_Common_Ancestor_of_a_Binary_Search_Tree;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public TreeNode? LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
|
||||||
|
{
|
||||||
|
int min = Math.Min(p.val, q.val);
|
||||||
|
int max = Math.Max(p.val, q.val);
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
if (root == null)
|
||||||
|
return null;
|
||||||
|
else if (min <= root.val && max>=root.val)
|
||||||
|
return root;
|
||||||
|
else if (root.val >= p.val)
|
||||||
|
root = root.left!;
|
||||||
|
else
|
||||||
|
root = root.right!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace _235._Lowest_Common_Ancestor_of_a_Binary_Search_Tree;
|
||||||
|
|
||||||
|
//Definition for a binary tree node.
|
||||||
|
public class TreeNode {
|
||||||
|
public int val;
|
||||||
|
public TreeNode? left;
|
||||||
|
public TreeNode? right;
|
||||||
|
public TreeNode(int x) { val = x; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_3._Longest_Substring_Without_Repeating_Characters</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace _3._Longest_Substring_Without_Repeating_Characters;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine(new Solution().LengthOfLongestSubstring("dvdf"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
namespace _3._Longest_Substring_Without_Repeating_Characters;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int LengthOfLongestSubstring(string s)
|
||||||
|
{
|
||||||
|
int max = 0;
|
||||||
|
Dictionary<char, int> dict = new Dictionary<char, int>();
|
||||||
|
for(int i = 0; i < s.Length; i++)
|
||||||
|
{
|
||||||
|
char c = s[i];
|
||||||
|
if(dict.ContainsKey(c))
|
||||||
|
{
|
||||||
|
max = Math.Max(max, dict.Count);
|
||||||
|
i = dict[c];
|
||||||
|
dict.Clear();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
dict.Add(c, i);
|
||||||
|
}
|
||||||
|
max = Math.Max(max, dict.Count);
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
567. Permutation in String/567. Permutation in String.csproj
Normal file
11
567. Permutation in String/567. Permutation in String.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_567._Permutation_in_String</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
9
567. Permutation in String/Program.cs
Normal file
9
567. Permutation in String/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace _567._Permutation_in_String;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine(new Solution().CheckInclusion("adc", "dcda"));
|
||||||
|
}
|
||||||
|
}
|
||||||
39
567. Permutation in String/Solution.cs
Normal file
39
567. Permutation in String/Solution.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
namespace _567._Permutation_in_String;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public bool CheckInclusion(string s1, string s2)
|
||||||
|
{
|
||||||
|
int[] letters = new int[26];
|
||||||
|
foreach (char c in s1)
|
||||||
|
letters[c-97]++;
|
||||||
|
int[] buff = new int[26];
|
||||||
|
Array.Copy(letters, buff, letters.Length);
|
||||||
|
for (int i = 0; i <= s2.Length-s1.Length; i++)
|
||||||
|
{
|
||||||
|
if (letters[s2[i]-97] != 0)
|
||||||
|
{
|
||||||
|
Array.Copy(letters, buff, letters.Length);
|
||||||
|
for(int j = i; j < i + s1.Length; j++)
|
||||||
|
{
|
||||||
|
char ch = s2[j];
|
||||||
|
buff[ch - 97]--;
|
||||||
|
if (buff[ch-97] < 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bool win = true;
|
||||||
|
for (int bIndex = 0; bIndex < buff.Length; bIndex++)
|
||||||
|
{
|
||||||
|
if (buff[bIndex] != 0)
|
||||||
|
{
|
||||||
|
win = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (win)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
11
695. Max Area of Island/695. Max Area of Island.csproj
Normal file
11
695. Max Area of Island/695. Max Area of Island.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_695._Max_Area_of_Island</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
10
695. Max Area of Island/Program.cs
Normal file
10
695. Max Area of Island/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace _695._Max_Area_of_Island;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
int[][] matrix = {new int[] {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},new int[] {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},new int[] {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},new int[] {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}};
|
||||||
|
Console.WriteLine(new Solution().MaxAreaOfIsland(matrix));
|
||||||
|
}
|
||||||
|
}
|
||||||
41
695. Max Area of Island/Solution.cs
Normal file
41
695. Max Area of Island/Solution.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
namespace _695._Max_Area_of_Island;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int MaxAreaOfIsland(int[][] grid)
|
||||||
|
{
|
||||||
|
bool[][] visited = new bool[grid.Length][];
|
||||||
|
for (int i = 0; i < visited.Length; i++)
|
||||||
|
visited[i] = new bool[grid[i].Length];
|
||||||
|
|
||||||
|
int max = 0;
|
||||||
|
for (int y = 0; y < grid.Length; y++)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < grid[y].Length; x++)
|
||||||
|
{
|
||||||
|
if (grid[y][x] == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int curCount = 0;
|
||||||
|
Recursion(grid, y, x, ref curCount, visited);
|
||||||
|
max = Math.Max(max, curCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Recursion(int[][] grid, int y, int x, ref int count, bool[][] visited)
|
||||||
|
{
|
||||||
|
if (visited[y][x] || grid[y][x] == 0)
|
||||||
|
return;
|
||||||
|
visited[y][x] = true;
|
||||||
|
count++;
|
||||||
|
for (int i = 1; i < 8; i += 2)
|
||||||
|
{
|
||||||
|
int nextY = i / 3 + y - 1;
|
||||||
|
int nextX = i % 3 + x - 1;
|
||||||
|
if (nextY >= 0 && nextY < grid.Length && nextX >= 0 && nextX < grid[nextY].Length)
|
||||||
|
Recursion(grid, nextY, nextX, ref count, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
733. Flood Fill/733. Flood Fill.csproj
Normal file
11
733. Flood Fill/733. Flood Fill.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_733._Flood_Fill</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
14
733. Flood Fill/Program.cs
Normal file
14
733. Flood Fill/Program.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
namespace _733._Flood_Fill;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
int[][] matrix = new int[][] { new int[] { 1,1,1 },
|
||||||
|
new int[] { 1,1,0 },
|
||||||
|
new int[] { 1,0,1 } };
|
||||||
|
var res = new Solution().FloodFill(matrix, 1,1, 2);
|
||||||
|
foreach(var row in res)
|
||||||
|
Console.WriteLine(String.Join(" ", row));
|
||||||
|
}
|
||||||
|
}
|
||||||
32
733. Flood Fill/Solution.cs
Normal file
32
733. Flood Fill/Solution.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
namespace _733._Flood_Fill;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int[][] FloodFill(int[][] image, int sr, int sc, int color)
|
||||||
|
{
|
||||||
|
int[][] res = new int[image.Length][];
|
||||||
|
for (int i = 0; i < res.Length; i++)
|
||||||
|
res[i] = image[i].ToArray();
|
||||||
|
|
||||||
|
bool[][] colored = new bool[image.Length][];
|
||||||
|
for (int i = 0; i < colored.Length; i++)
|
||||||
|
colored[i] = new bool[image[i].Length];
|
||||||
|
Recursion(res, sr, sc, res[sr][sc], color, colored);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Recursion(int[][] image, int y, int x, int sourceColor, int toColor, bool[][] colored)
|
||||||
|
{
|
||||||
|
if (image[y][x] != sourceColor || colored[y][x])
|
||||||
|
return;
|
||||||
|
image[y][x] = toColor;
|
||||||
|
colored[y][x] = true;
|
||||||
|
for (int i = 1; i < 8; i += 2)
|
||||||
|
{
|
||||||
|
int nextY = i / 3 + y - 1;
|
||||||
|
int nextX = i % 3 + x - 1;
|
||||||
|
if (nextY >= 0 && nextY < image.Length && nextX >= 0 && nextX < image[nextY].Length)
|
||||||
|
Recursion(image, nextY, nextX, sourceColor, toColor, colored);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_83._Remove_Duplicates_from_Sorted_List</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
37
83. Remove Duplicates from Sorted List/ListNode.cs
Normal file
37
83. Remove Duplicates from Sorted List/ListNode.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
namespace _83._Remove_Duplicates_from_Sorted_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
83. Remove Duplicates from Sorted List/Program.cs
Normal file
17
83. Remove Duplicates from Sorted List/Program.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
namespace _83._Remove_Duplicates_from_Sorted_List;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
ListNode l1 = new ListNode(1);
|
||||||
|
ListNode l2 = new ListNode(1);
|
||||||
|
ListNode l3 = new ListNode(4);
|
||||||
|
ListNode l4 = new ListNode(4);
|
||||||
|
|
||||||
|
l1.next = l2;
|
||||||
|
l2.next = l3;
|
||||||
|
l3.next = l4;
|
||||||
|
Console.WriteLine(new Solution().DeleteDuplicates(null));
|
||||||
|
}
|
||||||
|
}
|
||||||
26
83. Remove Duplicates from Sorted List/Solution.cs
Normal file
26
83. Remove Duplicates from Sorted List/Solution.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
namespace _83._Remove_Duplicates_from_Sorted_List;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public ListNode? DeleteDuplicates(ListNode? head)
|
||||||
|
{
|
||||||
|
ListNode? first = null;
|
||||||
|
ListNode? current = null;
|
||||||
|
while (head != null)
|
||||||
|
{
|
||||||
|
if (first == null)
|
||||||
|
first = current = head;
|
||||||
|
if (head.val == current!.val)
|
||||||
|
{
|
||||||
|
head = head.next;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
current!.next = head;
|
||||||
|
current = current.next;
|
||||||
|
head = head.next;
|
||||||
|
}
|
||||||
|
if(current != null)
|
||||||
|
current.next = null;
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<RootNamespace>_98._Validate_Binary_Search_Tree</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
9
98. Validate Binary Search Tree/Program.cs
Normal file
9
98. Validate Binary Search Tree/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace _98._Validate_Binary_Search_Tree;
|
||||||
|
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello, World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
21
98. Validate Binary Search Tree/Solution.cs
Normal file
21
98. Validate Binary Search Tree/Solution.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
namespace _98._Validate_Binary_Search_Tree;
|
||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public bool IsValidBST(TreeNode root)
|
||||||
|
{
|
||||||
|
return Rec(root, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool Rec(TreeNode node, int? lessThan, int? moreThan)
|
||||||
|
{
|
||||||
|
if((lessThan != null && node.val >= lessThan) || (moreThan != null && node.val <= moreThan))
|
||||||
|
return false;
|
||||||
|
bool res = true;
|
||||||
|
if(node.left != null)
|
||||||
|
res&=Rec(node.left, node.val, moreThan);
|
||||||
|
if(node.right != null)
|
||||||
|
res &=Rec(node.right, lessThan, node.val);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
16
98. Validate Binary Search Tree/TreeNode.cs
Normal file
16
98. Validate Binary Search Tree/TreeNode.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
namespace _98._Validate_Binary_Search_Tree;
|
||||||
|
|
||||||
|
|
||||||
|
//Definition for a binary tree node.
|
||||||
|
public class TreeNode
|
||||||
|
{
|
||||||
|
public int val;
|
||||||
|
public TreeNode? left;
|
||||||
|
public TreeNode? right;
|
||||||
|
public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null)
|
||||||
|
{
|
||||||
|
this.val = val;
|
||||||
|
this.left = left;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
}
|
||||||
66
Leetcode.sln
66
Leetcode.sln
@@ -71,11 +71,31 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "589. N-ary Tree Preorder Tr
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "102. Binary Tree Level Order Traversal", "102. Binary Tree Level Order Traversal\102. Binary Tree Level Order Traversal.csproj", "{557D8195-C886-4B52-BBC4-285A1C1057FF}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "102. Binary Tree Level Order Traversal", "102. Binary Tree Level Order Traversal\102. Binary Tree Level Order Traversal.csproj", "{557D8195-C886-4B52-BBC4-285A1C1057FF}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "141. Linked List Cycle", "141. Linked List Cycle\141. Linked List Cycle.csproj", "{EC089303-B1E1-482F-BAED-530DAB4C1346}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "141. Linked List Cycle", "141. Linked List Cycle\141. Linked List Cycle.csproj", "{EC089303-B1E1-482F-BAED-530DAB4C1346}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "203. Remove Linked List Elements", "203. Remove Linked List Elements\203. Remove Linked List Elements.csproj", "{F2C5A71D-91C7-4270-8150-8D96DA73538C}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "203. Remove Linked List Elements", "203. Remove Linked List Elements\203. Remove Linked List Elements.csproj", "{F2C5A71D-91C7-4270-8150-8D96DA73538C}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "19. Remove Nth Node From End of List", "19. Remove Nth Node From End of List\19. Remove Nth Node From End of List.csproj", "{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "19. Remove Nth Node From End of List", "19. Remove Nth Node From End of List\19. Remove Nth Node From End of List.csproj", "{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3. Longest Substring Without Repeating Characters", "3. Longest Substring Without Repeating Characters\3. Longest Substring Without Repeating Characters.csproj", "{B63CF842-6B6E-48BA-B9E5-CCCBC0AE34EC}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "567. Permutation in String", "567. Permutation in String\567. Permutation in String.csproj", "{AC0417D5-D2AE-4E55-92F1-384A03990678}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "733. Flood Fill", "733. Flood Fill\733. Flood Fill.csproj", "{6574DD3F-F45E-4AD2-9107-83A2829EF4A0}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "695. Max Area of Island", "695. Max Area of Island\695. Max Area of Island.csproj", "{7D77DF91-D338-401D-9553-DBD588C25EA4}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "83. Remove Duplicates from Sorted List", "83. Remove Duplicates from Sorted List\83. Remove Duplicates from Sorted List.csproj", "{7468301A-2AE8-415B-9680-D858B936ED47}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "20. Valid Parentheses", "20. Valid Parentheses\20. Valid Parentheses.csproj", "{B9251922-A37F-4897-9BF6-A68BB30D60CD}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "232. Implement Queue using Stacks", "232. Implement Queue using Stacks\232. Implement Queue using Stacks.csproj", "{A068ABAC-F412-4017-BD8B-E245FF289713}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "98. Validate Binary Search Tree", "98. Validate Binary Search Tree\98. Validate Binary Search Tree.csproj", "{DB61AA75-8C2C-4E16-8A8E-781B831BE81A}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "235. Lowest Common Ancestor of a Binary Search Tree", "235. Lowest Common Ancestor of a Binary Search Tree\235. Lowest Common Ancestor of a Binary Search Tree.csproj", "{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "200. Number of Islands", "200. Number of Islands\200. Number of Islands.csproj", "{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -231,6 +251,46 @@ Global
|
|||||||
{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}.Release|Any CPU.Build.0 = Release|Any CPU
|
{88473211-C7AD-4F83-92C2-D4E6B9BB9D8A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B63CF842-6B6E-48BA-B9E5-CCCBC0AE34EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B63CF842-6B6E-48BA-B9E5-CCCBC0AE34EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B63CF842-6B6E-48BA-B9E5-CCCBC0AE34EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B63CF842-6B6E-48BA-B9E5-CCCBC0AE34EC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{AC0417D5-D2AE-4E55-92F1-384A03990678}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{AC0417D5-D2AE-4E55-92F1-384A03990678}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{AC0417D5-D2AE-4E55-92F1-384A03990678}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{AC0417D5-D2AE-4E55-92F1-384A03990678}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{6574DD3F-F45E-4AD2-9107-83A2829EF4A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{6574DD3F-F45E-4AD2-9107-83A2829EF4A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{6574DD3F-F45E-4AD2-9107-83A2829EF4A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{6574DD3F-F45E-4AD2-9107-83A2829EF4A0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{7D77DF91-D338-401D-9553-DBD588C25EA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{7D77DF91-D338-401D-9553-DBD588C25EA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{7D77DF91-D338-401D-9553-DBD588C25EA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{7D77DF91-D338-401D-9553-DBD588C25EA4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{7468301A-2AE8-415B-9680-D858B936ED47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{7468301A-2AE8-415B-9680-D858B936ED47}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{7468301A-2AE8-415B-9680-D858B936ED47}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{7468301A-2AE8-415B-9680-D858B936ED47}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B9251922-A37F-4897-9BF6-A68BB30D60CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B9251922-A37F-4897-9BF6-A68BB30D60CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B9251922-A37F-4897-9BF6-A68BB30D60CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B9251922-A37F-4897-9BF6-A68BB30D60CD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{A068ABAC-F412-4017-BD8B-E245FF289713}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{A068ABAC-F412-4017-BD8B-E245FF289713}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{A068ABAC-F412-4017-BD8B-E245FF289713}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{A068ABAC-F412-4017-BD8B-E245FF289713}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{DB61AA75-8C2C-4E16-8A8E-781B831BE81A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{DB61AA75-8C2C-4E16-8A8E-781B831BE81A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{DB61AA75-8C2C-4E16-8A8E-781B831BE81A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{DB61AA75-8C2C-4E16-8A8E-781B831BE81A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user