This commit is contained in:
Electrominch
2022-10-07 00:48:29 +03:00
parent c388b14c3f
commit 0b88604037
20 changed files with 414 additions and 7 deletions

View File

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

View File

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

View File

@@ -0,0 +1,46 @@
namespace _167._Two_Sum_II___Input_Array_Is_Sorted;
public class Solution
{
public int[]? TwoSum(int[] numbers, int target)
{
for(int i = 0; i < numbers.Length; i++)
{
int cur = numbers[i];
int nextTar = target - cur;
int index = Search(numbers, nextTar);
if (i == index || index < 0 || index >= numbers.Length)
continue;
int val = numbers[index];
if (cur + val == target)
{
if(i>index)
{
i = i + index;
index = i - index;
i = i - index;
}
return new int[] { i + 1, index + 1 };
}
}
return null;
}
public int Search(int[] nums, int target)
{
int l = -1;
int r = nums.Length;
while (l < r - 1)
{
int i = l + (r - l) / 2;
int cur = nums[i];
if (cur == target)
return i;
if (cur > target)
r = i;
else
l = i;
}
return -1;
}
}

View File

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

View File

@@ -0,0 +1,11 @@
namespace _240._Search_a_2D_Matrix_II
{
internal class Program
{
static void Main(string[] args)
{
int[][] matrix = new int[][] { new int[] { 1, 1,1,1 } };
Console.WriteLine(new Solution().SearchMatrix(matrix, 2));
}
}
}

View File

@@ -0,0 +1,30 @@
namespace _240._Search_a_2D_Matrix_II;
public class Solution
{
public bool SearchMatrix(int[][] matrix, int target)
{
for (int row = 0; row < matrix.Length; row++)
{
int column = SearchInsert((i) => matrix[row][i], matrix[row].Length, target);
if (column < matrix[row].Length && matrix[row][column] == target)
return true;
}
return false;
}
private int SearchInsert(Func<int, int> getByIndex, int len, int target)
{
int l = -1;
int r = len;
while (l != r - 1)
{
int i = (r + l) / 2;
if (getByIndex(i) >= target)
r = i;
else
l = i;
}
return l + 1;
}
}

View File

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

View File

@@ -0,0 +1,12 @@
namespace _283._Move_Zeroes
{
internal class Program
{
static void Main(string[] args)
{
var test = new int[] { 0, 1, 0, 3, 12 };
new Solution().MoveZeroes(test);
Console.WriteLine(String.Join(" ", test));
}
}
}

View File

@@ -0,0 +1,20 @@
namespace _283._Move_Zeroes;
public class Solution
{
public void MoveZeroes(int[] nums)
{
for(int i = 0; i < nums.Length-1; i++)
{
if (nums[i] != 0)
continue;
int nonZeroIndex = i + 1;
while (nonZeroIndex < nums.Length && nums[nonZeroIndex] == 0)
nonZeroIndex++;
if (nonZeroIndex >= nums.Length)
break;
nums[i] = nums[nonZeroIndex];
nums[nonZeroIndex] = 0;
}
}
}

View File

@@ -4,7 +4,6 @@
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}

View File

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

View File

@@ -0,0 +1,19 @@
namespace _36._Valid_Sudoku
{
internal class Program
{
static void Main(string[] args)
{
var arr = new char[][] { new char[]{'.', '.', '4', '.', '.', '.', '6', '3', '.'},
new char[]{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
new char[]{'5', '.', '.', '.', '.', '.', '.', '9', '.'},
new char[]{'.', '.', '.', '5', '6', '.', '.', '.', '.'},
new char[]{'4', '.', '3', '.', '.', '.', '.', '.', '1'},
new char[]{'.', '.', '.', '7', '.', '.', '.', '.', '.'},
new char[]{'.', '.', '.', '5', '.', '.', '.', '.', '.'},
new char[]{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
new char[]{'.', '.', '.', '.', '.', '.', '.', '.', '.'}};
Console.WriteLine(new Solution().IsValidSudoku(arr));
}
}
}

View File

@@ -0,0 +1,80 @@
namespace _36._Valid_Sudoku;
public class Solution
{
public bool IsValidSudoku(char[][] board)
{
return CheckRows(board) && CheckColumns(board) && CheckSubBoxes(board);
}
private bool CheckRows(char[][] board)
{
foreach (char[] row in board)
{
HashSet<char> rowSet = new HashSet<char>();
foreach (char c in row)
{
if(c != '.')
{
int prevCount = rowSet.Count;
rowSet.Add(c);
if (prevCount == rowSet.Count)
return false;
}
}
}
return true;
}
private bool CheckColumns(char[][] board)
{
for(int columnIndex = 0; columnIndex < board[0].Length; columnIndex++)
{
HashSet<char> column = new HashSet<char>();
for(int rowIndex = 0; rowIndex < board[columnIndex].Length; rowIndex++)
{
char ch = board[rowIndex][columnIndex];
if (ch != '.')
{
int prevCount = column.Count;
column.Add(ch);
if (prevCount == column.Count)
return false;
}
}
}
return true;
}
private bool CheckSubBoxes(char[][] board)
{
for(int i = 0; i < 27; i+=3)
{
int y = i / 9 * 3;
int x = i % 9;
if (CheckSubBox(board, y, x) == false)
return false;
}
return true;
}
private bool CheckSubBox(char[][] board, int startY, int startX)
{
HashSet<char> set = new HashSet<char>();
for (int y = startY; y < startY+3; y++)
{
for(int x = startX; x < startX+3; x++)
{
char ch = board[y][x];
if(ch!='.')
{
int prev = set.Count;
set.Add(ch);
if (prev == set.Count)
return false;
}
}
}
return true;
}
}

View File

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

View File

@@ -0,0 +1,10 @@
namespace _409._Longest_Palindrome
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Solution().LongestPalindrome("bb"));
}
}
}

View File

@@ -0,0 +1,25 @@
namespace _409._Longest_Palindrome;
public class Solution
{
public int LongestPalindrome(string s)
{
Dictionary<char, int> dict = new Dictionary<char, int>();
foreach(var ch in s)
{
if(dict.ContainsKey(ch))
dict[ch]++;
else
dict.Add(ch, 1);
}
int sum = 0;
bool hasOdd = false;
foreach(var v in dict.Values)
{
sum += v / 2;
if(v % 2 == 1)
hasOdd = true;
}
return sum*2 + (hasOdd ? 1 : 0);
}
}

View File

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

View File

@@ -0,0 +1,11 @@
namespace _74._Search_a_2D_Matrix
{
internal class Program
{
static void Main(string[] args)
{
int[][] matrix = new int[][] { new int[] { 1 }};
Console.WriteLine(new Solution().SearchMatrix(matrix, 1));
}
}
}

View File

@@ -0,0 +1,30 @@
namespace _74._Search_a_2D_Matrix;
public class Solution
{
public bool SearchMatrix(int[][] matrix, int target)
{
int row = SearchInsert((i) => matrix[i][0], matrix.Length, target);
if (row < matrix.Length && matrix[row][0] == target)
return true;
if (row > 0)
row--;
int column = SearchInsert((i) => matrix[row][i], matrix[row].Length, target);
return column < matrix[row].Length && matrix[row][column] == target;
}
private int SearchInsert(Func<int, int> getByIndex, int len, int target)
{
int l = -1;
int r = len;
while (l != r - 1)
{
int i = (r + l) / 2;
if (getByIndex(i) >= target)
r = i;
else
l = i;
}
return l + 1;
}
}

View File

@@ -33,17 +33,29 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "278. First Bad Version", "2
EndProject
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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "142. Linked List Cycle II", "142. Linked List Cycle II\142. Linked List Cycle II.csproj", "{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "283. Move Zeroes", "283. Move Zeroes\283. Move Zeroes.csproj", "{E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "167. Two Sum II - Input Array Is Sorted", "167. Two Sum II - Input Array Is Sorted\167. Two Sum II - Input Array Is Sorted.csproj", "{9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "74. Search a 2D Matrix", "74. Search a 2D Matrix\74. Search a 2D Matrix.csproj", "{8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "240. Search a 2D Matrix II", "240. Search a 2D Matrix II\240. Search a 2D Matrix II.csproj", "{F47BA875-AF2F-4D00-AB75-A62A6E31387C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "409. Longest Palindrome", "409. Longest Palindrome\409. Longest Palindrome.csproj", "{F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "36. Valid Sudoku", "36. Valid Sudoku\36. Valid Sudoku.csproj", "{C1759C38-D2E4-4869-B6C2-259B26586E11}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -135,6 +147,30 @@ Global
{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
{E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Release|Any CPU.Build.0 = Release|Any CPU
{9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Release|Any CPU.Build.0 = Release|Any CPU
{8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Release|Any CPU.Build.0 = Release|Any CPU
{F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Release|Any CPU.Build.0 = Release|Any CPU
{F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Release|Any CPU.Build.0 = Release|Any CPU
{C1759C38-D2E4-4869-B6C2-259B26586E11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C1759C38-D2E4-4869-B6C2-259B26586E11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1759C38-D2E4-4869-B6C2-259B26586E11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1759C38-D2E4-4869-B6C2-259B26586E11}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE