lintcode459- Closest Number in Sorted Array- easy

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.

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.

用二分法模板写,想想可以知道最后一定在胜出的start,end两数之间,判断。

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;
        }

        int start = 0;
        int end = A.length - 1;

        while (start + 1 < end){
            int mid = start + (end - start) / 2;
            if (target == A[mid]){
                return mid;
            }
            else if (target < A[mid]){
                end = mid;
            }
            else {
                start = mid;
            }
        }

        if (Math.abs(A[start] - target) <= Math.abs(A[end] - target)){
            return start;
        }
        else {
            return end;
        }
    }
}
时间: 2024-10-12 20:27:30

lintcode459- Closest Number in Sorted Array- easy的相关文章

[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;

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

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

88. Merge Sorted Array [easy] (Python)

题目链接 https://leetcode.com/problems/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

leetcode_26_ Remove Duplicates from Sorted Array (easy)

Remove Duplicates from Sorted Array 题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem

Leetcode 26. Remove Duplicates from Sorted Array (easy)

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Exam

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

leetcodeRemove Duplicates from Sorted Array(easy) /java

我决定先刷easy. 这道题的诡异之处在于,不仅你得输出长度,你还得更改nums[]数组,把冗余的数清掉.比如 import java.io.*; import java.util.*; public class Solution { public static int removeDuplicates(int[] nums) { int r=0; int len=nums.length; int i=0,j=0; if(len<2) return 0; int cmp=nums[0]; int

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta