规范格式

This commit is contained in:
YDZ
2020-08-07 15:50:06 +08:00
parent 854a339abc
commit 4e11f4028a
1438 changed files with 907 additions and 924 deletions

View File

@ -0,0 +1,44 @@
# [719. Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/)
## 题目:
Given an integer array, return the k-th smallest **distance** among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
**Example 1:**
Input:
nums = [1,3,1]
k = 1
Output: 0
Explanation:
Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.
**Note:**
1. `2 <= len(nums) <= 10000`.
2. `0 <= nums[i] < 1000000`.
3. `1 <= k <= len(nums) * (len(nums) - 1) / 2`.
## 题目大意
给定一个整数数组,返回所有数对之间的第 k 个最小距离。一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值。
提示:
1. 2 <= len(nums) <= 10000.
2. 0 <= nums[i] < 1000000.
3. 1 <= k <= len(nums) * (len(nums) - 1) / 2.
## 解题思路
- 给出一个数组要求找出第 k 小两两元素之差的值两两元素之差可能重复重复的元素之差算多个不去重
- 这一题可以用二分搜索来解答先把原数组排序那么最大的差值就是 `nums[len(nums)-1] - nums[0]` 最小的差值是 0即在 `[0, nums[len(nums)-1] - nums[0]]` 区间内搜索最终答案针对每个 `mid`判断小于等于 `mid` 的差值有多少个题意就转化为在数组中找到这样一个数使得满足 `nums[i] - nums[j] ≤ mid` 条件的组合数等于 `k`那么如何计算满足两两数的差值小于 mid 的组合总数是本题的关键
- 最暴力的方法就是 2 重循环暴力计数这个方法效率不高耗时很长原因是没有利用数组有序这一条件实际上数组有序对计算满足条件的组合数有帮助利用双指针滑动即可计算出组合总数见解法一