fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14.  
  15. public int search(int[] nums, int target) {
  16. int low = 0, high = nums.length - 1;
  17.  
  18. while (low <= high) {
  19. int mid = low + (high - low) / 2;
  20.  
  21. if (nums[mid] == target) return mid;
  22.  
  23. // Left half is sorted
  24. if (nums[low] <= nums[mid]) {
  25. if ( target >= nums[low] && target < nums[mid]) {
  26. high = mid - 1; // target is in left half
  27. } else {
  28. low = mid + 1; // target is in right half
  29. }
  30. }
  31. // Right half is sorted
  32. else {
  33. if (target > nums[mid] && target <= nums[high]) {
  34. low = mid + 1; // target is in right half
  35. } else {
  36. high = mid - 1; // target is in left half
  37. }
  38. }
  39. }
  40.  
  41. return -1;
  42. }
  43. }
Success #stdin #stdout 0.08s 54560KB
stdin
Standard input is empty
stdout
Standard output is empty