mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
49 lines
1.6 KiB
HTML
49 lines
1.6 KiB
HTML
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,请你返回数对 <code>(i, j)</code> 的数目,满足 <code>i < j</code> 且 <code>|nums[i] - nums[j]| == k</code> 。</p>
|
||
|
||
<p><code>|x|</code> 的值定义为:</p>
|
||
|
||
<ul>
|
||
<li>如果 <code>x >= 0</code> ,那么值为 <code>x</code> 。</li>
|
||
<li>如果 <code>x < 0</code> ,那么值为 <code>-x</code> 。</li>
|
||
</ul>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<pre><b>输入:</b>nums = [1,2,2,1], k = 1
|
||
<b>输出:</b>4
|
||
<b>解释:</b>差的绝对值为 1 的数对为:
|
||
- [<em><strong>1</strong></em>,<em><strong>2</strong></em>,2,1]
|
||
- [<em><strong>1</strong></em>,2,<em><strong>2</strong></em>,1]
|
||
- [1,<em><strong>2</strong></em>,2,<em><strong>1</strong></em>]
|
||
- [1,2,<em><strong>2</strong></em>,<em><strong>1</strong></em>]
|
||
</pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<pre><b>输入:</b>nums = [1,3], k = 3
|
||
<b>输出:</b>0
|
||
<b>解释:</b>没有任何数对差的绝对值为 3 。
|
||
</pre>
|
||
|
||
<p><strong>示例 3:</strong></p>
|
||
|
||
<pre><b>输入:</b>nums = [3,2,1,5,4], k = 2
|
||
<b>输出:</b>3
|
||
<b>解释:</b>差的绝对值为 2 的数对为:
|
||
- [<em><strong>3</strong></em>,2,<em><strong>1</strong></em>,5,4]
|
||
- [<em><strong>3</strong></em>,2,1,<em><strong>5</strong></em>,4]
|
||
- [3,<em><strong>2</strong></em>,1,5,<em><strong>4</strong></em>]
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= nums.length <= 200</code></li>
|
||
<li><code>1 <= nums[i] <= 100</code></li>
|
||
<li><code>1 <= k <= 99</code></li>
|
||
</ul>
|