[LintCode] 459 Closest Number in Sorted Array

Description
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.

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

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.

Challenge
O(logn) time complexity.

4/23/2017

算法班,直接套用解题格式

注意第16,17行把超越输入范围的也包含进去,否则最后一行需要用绝对值来判断。

 1 public int totalOccurance(int[] A, int target) {
 2     if (A == null || A.length == 0) return -1;
 3     int ret;
 4     int start = 0, end = A.length - 1;
 5
 6     while (start + 1 < end) {
 7         int mid = start + (end - start) / 2;
 8         if (A[mid] == target) return mid;
 9         if (A[mid[ < target) {
10             end = mid;
11         } else {
12             start = mid;
13         }
14     }
15
16     if (A[start] >= target) return start;
17     if (A[end] <= target) return end;
18     return Math.abs(A[end] - target) < Math.abs(target - A[start])? end: start;
19 }    
时间: 2024-10-10 18:29:33

[LintCode] 459 Closest Number in Sorted 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;

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

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (

LintCode : Find Minimum in Rotated Sorted Array

public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] num) { // write your code here // handle corner case // the idea is to find the [first] element less than num[num.

[Lintcode] Kth Smallest Number in Sorted Matrix

Kth Smallest Number in Sorted Matrix Find the kth smallest number in at row and column sorted matrix. Have you met this question in a real interview? Yes Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k lo

K Closest Numbers In Sorted Array

Given a target number, a non-negative integer k and an integer array A sorted in ascending order, find the k closest numbers to target in A, sorted in ascending order by the difference between the number and target. Otherwise, sorted in ascending ord

lintcode 容易题:Merge Sorted Array II 合并排序数组 II

题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] 注意 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 解题: 这里给的是两个数组,上题给的是ArrayList格式,比较好处理,重新定义一个长度m+n的数组,太无节操,其他方法一时想不起来 官方解题,方法很技巧,可以倒着排序,这样就很简单了 Java程序: class

【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

[lintcode easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin