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 target = 2, return 1.

Given [1, 4, 6] and target = 3, return 1.

Given [1, 4, 6] and target = 5, return 1 or 2.

Given [1, 3, 3, 4] and target = 2, return 0 or 1 or 2.

Note

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

 1 class Solution {
 2 public:
 3     /**
 4      * @param A an integer array sorted in ascending order
 5      * @param target an integer
 6      * @return an integer
 7      */
 8     int closestNumber(vector<int>& A, int target) {
 9         if (A.size() == 0) {
10             return -1;
11         }
12
13         int b = 0, e = A.size() - 1;
14         while (b + 1 < e) {
15             int mid = b + (e - b) / 2;
16             if (A[mid] == target) {
17                 return mid;
18             } else if (A[mid] < target) {
19                 b = mid;
20             } else {
21                 e = mid;
22             }
23         }
24         if (abs(A[b] - target) < abs(A[e] - target)) {
25             return b;
26         } else {
27             return e;
28         }
29     }
30 };
时间: 2024-12-06 17:53:49

Closet Number in sorted array的相关文章

[LintCode] 459 Closest Number in Sorted Array

DescriptionGiven 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. NoticeThere can be duplicate elements in the array,

Closest Number in Sorted Array

public class Solution { /** * @param A an integer array sorted in ascending order * @param target an integer * @return an integer */ public int closestNumber(int[] A, int target) { // Write your code here if (A == null || A.length == 0) { return -1;

Count the number of occurrences in a sorted array

Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

【Lintcode】062.Search 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). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

88. 合并两个已排序的数组 Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initi

Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initi

Leetcode题目:Merge Sorted Array

题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements i

【数组】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. 思路: 这题其实就是找翻转后右侧部分的最左端点,典型的二分搜索变体,大致思路分成两种情况讨论