/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * class MountainArray { * public: * int get(int index); * int length(); * }; */ classSolution { public: intfindInMountainArray(int target, MountainArray &mountainArr){ int lo = 0, hi = mountainArr.length(); while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mid == mountainArr.length() - 1) { hi = mid; continue; } if (mountainArr.get(mid) < mountainArr.get(mid + 1)) lo = mid + 1; else hi = mid; } int mountain = lo; // cout << "mountain: " << mountain << endl; lo = 0, hi = mountain + 1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mountainArr.get(mid) < target) lo = mid + 1; else hi = mid; } // cout << "first: " << lo << endl; if (mountainArr.get(lo) == target) return lo; lo = mountain, hi = mountainArr.length(); while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mountainArr.get(mid) > target) lo = mid + 1; else hi = mid; } // cout << "second: " << lo << endl; if (lo < mountainArr.length() && mountainArr.get(lo) == target) return lo; return-1; } };