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