五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

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

LeetCode 2200. Find All K-Distant Indices in an Array

2023-06-01 11:46 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?integer array?nums?and two integers?key?and?k. A?k-distant index?is an index?i?of?nums?for which there exists at least one index?j?such that?|i - j| <= k?and?nums[j] == key.

Return?a list of all k-distant indices sorted in?increasing order.

?

Example 1:

Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1Output: [1,2,3,4,5,6]Explanation: Here, nums[2] == key and nums[5] == key. - For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j where |0 - j| <= k and nums[j] == key. Thus, 0 is not a k-distant index. - For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index. - For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index. - For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index. - For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index. - For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index. - For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.

Example 2:

Input: nums = [2,2,2,2,2], key = 2, k = 2Output: [0,1,2,3,4]Explanation: For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index. Hence, we return [0,1,2,3,4].

?

Constraints:

  • 1 <= nums.length <= 1000

  • 1 <= nums[i] <= 1000

  • key?is an integer from the array?nums.

  • 1 <= k <= nums.length

把所有等于Key的index放到set中,然后做一個(gè)函數(shù),去判斷 i是否在set的±k的區(qū)間內(nèi),在的話,放到list中,最后返回list;

Runtime:?10 ms, faster than?54.59%?of?Java?online submissions for?Find All K-Distant Indices in an Array.

Memory Usage:?44.2 MB, less than?6.99%?of?Java?online submissions for?Find All K-Distant Indices in an Array.


LeetCode 2200. Find All K-Distant Indices in an Array的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
黑山县| 蓝田县| 昌乐县| 肇庆市| 禹州市| 澎湖县| 左权县| 宜城市| 合山市| 鄱阳县| 舟山市| 泾源县| 高州市| 崇义县| 手机| 万宁市| 北辰区| 夏津县| 伽师县| 祥云县| 丹棱县| 南皮县| 孝感市| 京山县| 漳州市| 利津县| 松潘县| 高陵县| 保靖县| 汉阴县| 呼和浩特市| 枞阳县| 建德市| 天全县| 临西县| 五莲县| 舒城县| 馆陶县| 宜川县| 玉门市| 太保市|