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>_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;
}
}