mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			36 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Given a <strong>sorted</strong> integer array <code>arr</code>, two integers <code>k</code> and <code>x</code>, return the <code>k</code> closest integers to <code>x</code> in the array. The result should also be sorted in ascending order.</p>
 | 
						|
 | 
						|
<p>An integer <code>a</code> is closer to <code>x</code> than an integer <code>b</code> if:</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>|a - x| < |b - x|</code>, or</li>
 | 
						|
	<li><code>|a - x| == |b - x|</code> and <code>a < b</code></li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">arr = [1,2,3,4,5], k = 4, x = 3</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">[1,2,3,4]</span></p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">arr = [1,1,2,3,4,5], k = 4, x = -1</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">[1,1,2,3]</span></p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= k <= arr.length</code></li>
 | 
						|
	<li><code>1 <= arr.length <= 10<sup>4</sup></code></li>
 | 
						|
	<li><code>arr</code> is sorted in <strong>ascending</strong> order.</li>
 | 
						|
	<li><code>-10<sup>4</sup> <= arr[i], x <= 10<sup>4</sup></code></li>
 | 
						|
</ul>
 |