int FindSortedArrayMax(int A[], int N) { int L = 0; int R = N - 1; while (A[L] > A[R]) { int M = L + (R - L) / 2; if (A[M] > A[R]) L = M + 1; else R = M; } return L == 0 ? A[N-1] : A[L-1]; }
B*1
7 楼
Your code seems not right for this case: 3 5 1 3 3
【在 a*******y 的大作中提到】 : int FindSortedArrayMax(int A[], int N) { : int L = 0; : int R = N - 1; : while (A[L] > A[R]) { : int M = L + (R - L) / 2; : if (A[M] > A[R]) : L = M + 1; : else : R = M; : }
l*a
8 楼
obviously, it just doesn't support dup once there is dup, u need a full scan
【在 B*******1 的大作中提到】 : Your code seems not right for this case: : 3 5 1 3 3
B*1
9 楼
谢谢,我傻了。
【在 l*****a 的大作中提到】 : obviously, it just doesn't support dup : once there is dup, u need a full scan
C*y
10 楼
谢谢! 但是感觉不太对,比如说3,4,5,1,2 M = 2, A[2] > A[4] ==> L = 3 L实际上应该等于2 我稍微修改了一下你的程序,如下: int FindSortedArrayMax(int A[], int N) { int L = 0; int R = N - 1; while (Lif(A[L] < A[R]) return R; int M = L + (R - L) / 2; if (A[M] > A[R]) { if(L == M) break; L = M; } else R = M -1; } return L; }
【在 a*******y 的大作中提到】 : int FindSortedArrayMax(int A[], int N) { : int L = 0; : int R = N - 1; : while (A[L] > A[R]) { : int M = L + (R - L) / 2; : if (A[M] > A[R]) : L = M + 1; : else : R = M; : }
g*u
11 楼
看我的这个行不行? #include #include using namespace std; int maxShiftedSortArray(vector& array){ int n=array.size(); if(n==1) return array[0]; int l=0; int r=n-1; int m; while(lm=(r+l)/2; if(array[m]>=array[l]){ l=m; }else{ r=m-1; } } return max(array[l],array[r]); }
【在 C***y 的大作中提到】 : 谢谢! : 但是感觉不太对,比如说3,4,5,1,2 : M = 2, A[2] > A[4] ==> L = 3 : L实际上应该等于2 : 我稍微修改了一下你的程序,如下: : int FindSortedArrayMax(int A[], int N) { : int L = 0; : int R = N - 1; : while (L: if(A[L] < A[R]) return R;