-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.php
38 lines (32 loc) · 919 Bytes
/
BinarySearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
require_once 'helpers.php';
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
function search($nums, $target) {
$left = 0;
$right = count($nums) - 1;
while ($left <= $right) {
$mid = $left + floor(($right - $left) / 2);
if ($nums[$mid] == $target)
return (int) $mid; //Do a parse, cause it was returning a float number
elseif ($nums[$mid] < $target)
$left = $mid + 1;
else
$right = $mid - 1;
}
return -1;
}
}
$solution = new Solution();
$nums = [-1,0,3,5,9,12];
$target = 9;
$expectedOutput = 4;
checkResult($solution->search($nums, $target) === $expectedOutput, 1);
$nums = [-1,0,3,5,9,12];
$target = 2;
$expectedOutput = -1;
checkResult($solution->search($nums, $target) === $expectedOutput, 2);