Day5
This commit is contained in:
11
240. Search a 2D Matrix II/240. Search a 2D Matrix II.csproj
Normal file
11
240. Search a 2D Matrix II/240. Search a 2D Matrix II.csproj
Normal 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>
|
||||
11
240. Search a 2D Matrix II/Program.cs
Normal file
11
240. Search a 2D Matrix II/Program.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
30
240. Search a 2D Matrix II/Solution.cs
Normal file
30
240. Search a 2D Matrix II/Solution.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user