无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(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的評論 (共 條)

分享到微博請遵守國家法律
长泰县| 渑池县| 恩施市| 辽阳市| 襄垣县| 鱼台县| 汉阴县| 德令哈市| 南溪县| 金昌市| 喜德县| 潮安县| 曲阜市| 广西| 通化市| 福建省| 佛山市| 青阳县| 沂源县| 泗水县| 长治县| 邯郸市| 游戏| 安西县| 延庆县| 托里县| 卓尼县| 察雅县| 郎溪县| 宁陵县| 长阳| 裕民县| 新晃| 昌黎县| 当雄县| 江源县| 祁连县| 商水县| 海南省| 原平市| 纳雍县|