Day3. Start Algorithm
This commit is contained in:
11
704. Binary Search/704. Binary Search.csproj
Normal file
11
704. Binary Search/704. Binary Search.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_704._Binary_Search</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
10
704. Binary Search/Program.cs
Normal file
10
704. Binary Search/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace _704._Binary_Search
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(new Solution().Search(new int[] { -1, 0, 3, 5, 9, 12 }, 13));
|
||||
}
|
||||
}
|
||||
}
|
||||
22
704. Binary Search/Solution.cs
Normal file
22
704. Binary Search/Solution.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace _704._Binary_Search;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user