LeetCode Find K Closest Elements

原题链接在这里:https://leetcode.com/problems/find-k-closest-elements/description/

题目:

Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.

Example 1:

Input: [1,2,3,4,5], k=4, x=3
Output: [1,2,3,4]

Example 2:

Input: [1,2,3,4,5], k=4, x=-1
Output: [1,2,3,4]

Note:

  1. The value k is positive and will always be smaller than the length of the sorted array.
  2. Length of the given array is positive and will not exceed 104
  3. Absolute value of elements in the array and x will not exceed 104

题解:

直接查找这k个最近值开始的位置po. 也就是arr[po] 比 arr[po+k]更接近目标值x.

Time Complexity: O(logn + k).

Space: O(1). regardless res.

AC Java:

 1 class Solution {
 2     public List<Integer> findClosestElements(int[] arr, int k, int x) {
 3         List<Integer> res = new ArrayList<Integer>();
 4         if(arr == null || arr.length == 0 || k == 0){
 5             return res;
 6         }
 7
 8         int l = 0;
 9         int r = arr.length-k;
10         while(l<r){
11             int mid = l+(r-l)/2;
12             if(x - arr[mid] > arr[mid+k]-x){
13                 l = mid+1;
14             }else{
15                 r = mid;
16             }
17         }
18
19         for(int i = l; i<l+k; i++){
20             res.add(arr[i]);
21         }
22         return res;
23     }
24 }
时间: 2024-10-04 18:04:15

LeetCode Find K Closest Elements的相关文章

leetcode 658. Find K Closest Elements

Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. Example 1: Input: [1,2,3,4,5], k=4, x=3 Outp

658. Find K Closest Elements

Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. Example 1: Input: [1,2,3,4,5], k=4, x=3 Outp

LeetCode 973. K Closest Points to Origin

原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return

(双指针、二分Binary Search) leetcode 658. Find K closest Elements

题意:给定一个升序排列的数组,找到k个与x最相近的元素(即差值最小),返回的结果必须要是按升序排好的.如果有两个数与 x的差值一样,优先选择数值较小的那个数. 解法一:双指针(排除法),一个一个删,因为是有序数组,且返回的是连续升序子数组,所以每一次删除的元素一定是位于边界:如果数组含有共 7 个元素,要保留 3 个元素,因此要删除 4 个元素(arr.size()-k):因为要删除的元素都位于边界,于是可以使用双指针(左指针指向数组的第一个元素,右指针指向数组最后一个元素)对撞的方式确定保留区

[LeetCode] Top K Frequent Elements 前K个高频元素

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements.    Your algorithm's time complexity must be

[LeetCode]Top K Frequent Elements

题目描述: Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must

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

LeetCode OJ 347. Top K Frequent Elements hashmap+排序求解

题目链接:https://leetcode.com/problems/top-k-frequent-elements/. 347. Top K Frequent Elements My Submissions QuestionEditorial Solution Total Accepted: 15510 Total Submissions: 36453 Difficulty: Medium Given a non-empty array of integers, return the k mo

[LeetCode][Python]Top K Frequent Elements

op K Frequent Elements Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time