mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			50 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of pairs</em> <code>(i, j)</code> <em>where</em> <code>i < j</code> <em>such that</em> <code>|nums[i] - nums[j]| == k</code>.</p>
 | 
						|
 | 
						|
<p>The value of <code>|x|</code> is defined as:</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>x</code> if <code>x >= 0</code>.</li>
 | 
						|
	<li><code>-x</code> if <code>x < 0</code>.</li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [1,2,2,1], k = 1
 | 
						|
<strong>Output:</strong> 4
 | 
						|
<strong>Explanation:</strong> The pairs with an absolute difference of 1 are:
 | 
						|
- [<strong><u>1</u></strong>,<strong><u>2</u></strong>,2,1]
 | 
						|
- [<strong><u>1</u></strong>,2,<strong><u>2</u></strong>,1]
 | 
						|
- [1,<strong><u>2</u></strong>,2,<strong><u>1</u></strong>]
 | 
						|
- [1,2,<strong><u>2</u></strong>,<strong><u>1</u></strong>]
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [1,3], k = 3
 | 
						|
<strong>Output:</strong> 0
 | 
						|
<strong>Explanation:</strong> There are no pairs with an absolute difference of 3.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong class="example">Example 3:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> nums = [3,2,1,5,4], k = 2
 | 
						|
<strong>Output:</strong> 3
 | 
						|
<b>Explanation:</b> The pairs with an absolute difference of 2 are:
 | 
						|
- [<strong><u>3</u></strong>,2,<strong><u>1</u></strong>,5,4]
 | 
						|
- [<strong><u>3</u></strong>,2,1,<strong><u>5</u></strong>,4]
 | 
						|
- [3,<strong><u>2</u></strong>,1,5,<strong><u>4</u></strong>]
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</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>
 |