@Test public void testEmptyInputArray() { expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("eee"); f.find(new int[]{}, 0); }
public int find(int[] nums, int target) { if (nums == null || nums.length == 0) { throw new IllegalArgumentException("input array can not be null or empty!"); } if (!isOrdered(nums)) { throw new IllegalArgumentException("input array must be ordered!"); } int last = nums[nums.length - 1]; if (target > last) { throw new IllegalArgumentException( "There is no value in the input array equal to or greater than target: " + target + "\n" + "The maximum element in array is " + last); } if (target == last) { return last; } if (target <= nums[0]) { return nums[0]; } int lo = 0; int hi = nums.length - 1; int targetIdx = 0; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; if (target > nums[mid]) { lo = mid + 1; } else if (target > nums[mid - 1]) { targetIdx = mid; break; } else { hi = mid - 1; } } return nums[targetIdx]; }
FindFirstGreaterEqualEleInOrderedArray { public int find(int[] nums, int target) { if (nums == null || nums.length == 0) { throw new IllegalArgumentException("input array can not be null or empty!"); } if (!isOrdered(nums)) { throw new IllegalArgumentException("input array must be ordered!"); } int last = nums[nums.length - 1]; if (target > last) { throw new IllegalArgumentException( "There is no value in the input array equal to or greater than target: " + target + "\n" + "The maximum element in array is " + last); } if (target == last) { return last; } if (target <= nums[0]) { return nums[0]; } int lo = 0; int hi = nums.length - 1; int targetIdx = 0; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; if (target > nums[mid]) { lo = mid + 1; } else if (target > nums[mid - 1]) { targetIdx = mid; break; } else { hi = mid - 1; } } return nums[targetIdx]; } }
FindFirstGreaterEqualEleInOrderedArray { public int find(int[] nums, int target) { if (nums == null || nums.length == 0) { throw new IllegalArgumentException("input array can not be null or empty!"); } if (!isOrdered(nums)) { throw new IllegalArgumentException("input array must be ordered!"); } int last = nums[nums.length - 1]; if (target > last) { throw new IllegalArgumentException( "There is no value in the input array equal to or greater than target: " + target + "\n" + "The maximum element in array is " + last); } if (target == last) { return last; } if (target <= nums[0]) { return nums[0]; } int lo = 0; int hi = nums.length - 1; int targetIdx = 0; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; if (target > nums[mid]) { lo = mid + 1; } else if (target > nums[mid - 1]) { targetIdx = mid; break; } else { hi = mid - 1; } } return nums[targetIdx]; }  }
FindFirstGreaterEqualEleInOrderedArray { public int find(int[] nums, int target) { if (nums == null || nums.length == 0) { throw new IllegalArgumentException("input array can not be null or empty!"); } if (!isOrdered(nums)) { throw new IllegalArgumentException("input array must be ordered!"); } int last = nums[nums.length - 1]; if (target > last) { throw new IllegalArgumentException( "There is no value in the input array equal to or greater than target: " + target + "\n" + "The maximum element in array is " + last); } if (target == last) { return last; } if (target <= nums[0]) { return nums[0]; } int lo = 0; int hi = nums.length - 1; int targetIdx = 0; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; if (target > nums[mid]) { lo = mid + 1; } else if (target > nums[mid - 1]) { targetIdx = mid; break; } else { hi = mid - 1; } } return nums[targetIdx]; }  static void main(String[] args); int find(int[] nums, int target); }
FindFirstGreaterEqualEleInOrderedArray { public int find(int[] nums, int target) { if (nums == null || nums.length == 0) { throw new IllegalArgumentException("input array can not be null or empty!"); } if (!isOrdered(nums)) { throw new IllegalArgumentException("input array must be ordered!"); } int last = nums[nums.length - 1]; if (target > last) { throw new IllegalArgumentException( "There is no value in the input array equal to or greater than target: " + target + "\n" + "The maximum element in array is " + last); } if (target == last) { return last; } if (target <= nums[0]) { return nums[0]; } int lo = 0; int hi = nums.length - 1; int targetIdx = 0; while (lo <= hi) { int mid = (hi - lo) / 2 + lo; if (target > nums[mid]) { lo = mid + 1; } else if (target > nums[mid - 1]) { targetIdx = mid; break; } else { hi = mid - 1; } } return nums[targetIdx]; }  static void main(String[] args); int find(int[] nums, int target);  }