MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/medium/_0033/Solution.java:11–32  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/16 desc :

Source from the content-addressed store, hash-verified

9 * </pre>
10 */
11public class Solution {
12 public int search(int[] nums, int target) {
13 int l = 0, r = nums.length - 1, mid;
14 while (l <= r) {
15 mid = l + r >>> 1;
16 if (nums[mid] == target) return mid;
17 else if (nums[mid] >= nums[l]) {
18 if (nums[l] <= target && target < nums[mid]) r = mid - 1;
19 else l = mid + 1;
20 } else {
21 if (nums[mid] < target && target <= nums[r]) l = mid + 1;
22 else r = mid - 1;
23 }
24 }
25 return -1;
26 }
27
28 public static void main(String[] args) {
29 Solution solution = new Solution();
30 System.out.println(solution.search(new int[]{2, 1}, 1));
31 }
32}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected