-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht17.php
More file actions
32 lines (31 loc) · 855 Bytes
/
t17.php
File metadata and controls
32 lines (31 loc) · 855 Bytes
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
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h1>旋转数组搜索</h1>
<h2>给定一个有序数组的旋转和一个目标值,返回目标值在该数组的下标,如果不存在,那么返回-1,假设该数组没有重复的值</h2>
<?php
function searchRotatedArry(&$arrA,$target){
$left=0;
$right=count($arrA)-1;
while ($left<$right) {
$mid=$left+intval(($right-$left)/2);
if($arrA[$mid]==$target){
return $mid;
}
if($arrA[$left]<=$arrA[$mid]){
if($arrA[$left]<=$target && $target <$arrA[$mid]){
$right=$mid-1;
}else{
$left=$mid+1;
}
}else{
if($arrA[$mid]<$target&&$target<=$arrA[$right]){
$left=$mid+1;
}else{
$right=$mid-1;
}
}
}
return -1;
}
$a=array(3,4,5,6,1,2);
echo searchRotatedArry($a,5).'<br>';
echo searchRotatedArry($a,7);