Closest In Sorted Array - Medium

Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T.

Assumptions

  • There can be duplicate elements in the array, and we can return any of the indices with same value.

Examples

  • A = {1, 2, 3}, T = 2, return 1
  • A = {1, 4, 6}, T = 3, return 1
  • A = {1, 4, 6}, T = 5, return 1 or 2
  • A = {1, 3, 3, 4}, T = 2, return 0 or 1 or 2

Corner Cases

  • What if A is null or A is of zero length? We should return -1 in this case.

time: O(log(n)), space: O(1)

public class Solution {
  public int closest(int[] array, int target) {
    // Write your solution here
    if(array == null || array.length == 0) return -1;
    int left = 0, right = array.length - 1;
    while(left + 1 < right) {
      int mid = left + (right - left) / 2;
      if(array[mid] == target)
        return mid;
      else if(array[mid] < target)
        left = mid;
      else
        right = mid;
    }
    return Math.abs(array[left] - target) < Math.abs(array[right] - target) ? left : right;
  }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10124211.html

时间: 2024-10-09 01:27:08

Closest In Sorted Array - Medium的相关文章

K Closest In Sorted Array - Medium

Given a target integer T, a non-negative integer K and an integer array A sorted in ascending order, find the K closest numbers to T in A. Assumptions A is not null K is guranteed to be >= 0 and K is guranteed to be <= A.length Return A size K integ

Closest In Sorted Array

Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T. Assumptions There can be duplicate elements in the array, and we can return any of the indices with same value. Examples

33. Search in Rotated Sorted Array - Medium

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return -1.

62. Search in Rotated Sorted Array【medium】

62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, ot

LeetCode80 Remove Duplicates from Sorted Array II

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? (Medium) For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

Closet Number in sorted array

Closest Number in Sorted Array Given a target number and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to the given target. Return -1 if there is no element in the array. Example Given [1, 2, 3] and tar

&lt;LeetCode OJ&gt; Find Minimum in Rotated Sorted Array【153】

153. Find Minimum in Rotated Sorted Array My Submissions Question Total Accepted: 73048 Total Submissions: 209952 Difficulty: Medium Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7

LeetCode153:Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 不知道这道题为什么难度是Medium,感觉蛮简单的. 仅仅须要找到第一个大于它后面的数,它后面的数就