Skip to content

Commit

Permalink
create 35 Search Insert Posicion c#
Browse files Browse the repository at this point in the history
  • Loading branch information
thuanle123 committed Jan 7, 2023
1 parent 9bc447b commit 14f9ff7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions csharp/0035-search-insert-position.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Solution
{
public int SearchInsert(int[] nums, int target)
{
int leftPointer = 0;
int rightPointer = nums.Length - 1;
while (leftPointer <= rightPointer)
{
int mid = leftPointer + (rightPointer - leftPointer) / 2;
if (nums[mid] == target)
{
return mid;
}
else if (nums[mid] < target)
{
leftPointer = mid + 1;
}
else
{
rightPointer = mid - 1;
}
}
return leftPointer;
}
}

0 comments on commit 14f9ff7

Please sign in to comment.