国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 658. Find K Closest Elements

2023-04-21 15:49 作者:您是打尖兒還是住店呢  | 我要投稿

Given a?sorted?integer array?arr, two integers?k?and?x, return the?k?closest integers to?x?in the array. The result should also be sorted in ascending order.

An integer?a?is closer to?x?than an integer?b?if:

  • |a - x| < |b - x|, or

  • |a - x| == |b - x|?and?a < b

?

Example 1:

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

Output: [1,2,3,4]

Example 2:

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

Output: [1,2,3,4]

?

Constraints:

  • 1 <= k <= arr.length

  • 1 <= arr.length <= 104

  • arr?is sorted in?ascending?order.

  • -104?<= arr[i], x <= 104

我是先把跟x的差值的絕對值放到二維數(shù)組中,然后對應(yīng)的值放到另一個位置上,依次排序,

然后找出最接近的k個數(shù)值即可,然后把數(shù)字放到list中就行,其實多操作了一次,沒必要再放到k長度的數(shù)組中,直接放到list中即可。

下面是代碼:

Runtime:?25 ms, faster than?23.48%?of?Java?online submissions for?Find K Closest Elements.

Memory Usage:?43.7 MB, less than?98.88%?of?Java?online submissions for?Find K Closest Elements.


LeetCode 658. Find K Closest Elements的評論 (共 條)

分享到微博請遵守國家法律
化德县| 天镇县| 丘北县| 泰和县| 蓝山县| 沂水县| 屯昌县| 榆林市| 新闻| 荔波县| 商南县| 太保市| 察哈| 台南市| 澎湖县| 黎城县| 微山县| 夏津县| 顺昌县| 肥东县| 林口县| 秦皇岛市| 诸城市| 福州市| 磐石市| 昭平县| 会昌县| 南昌县| 奇台县| 青神县| 东阿县| 汤阴县| 阳原县| 南昌市| 长兴县| 中西区| 金门县| 肃北| 甘孜县| 佳木斯市| 榆树市|