1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/找到按位与最接近 K 的子数组 [find-subarray-with-bitwise-and-closest-to-k].html
2024-06-05 08:50:06 +08:00

54 lines
2.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;。你需要找到&nbsp;<code>nums</code>&nbsp;的一个&nbsp;<span data-keyword="subarray-nonempty">子数组</span>&nbsp;,满足子数组中所有元素按位与运算&nbsp;<code>AND</code>&nbsp;的值与 <code>k</code>&nbsp;<strong>绝对差</strong>&nbsp;尽可能 <strong></strong>&nbsp;。换言之,你需要选择一个子数组&nbsp;<code>nums[l..r]</code>&nbsp;满足&nbsp;<code>|k - (nums[l] AND nums[l + 1] ... AND nums[r])|</code>&nbsp;最小。</p>
<p>请你返回 <strong>最小</strong>&nbsp;的绝对差值。</p>
<p><strong>子数组</strong>是数组中连续的&nbsp;<strong>非空</strong>&nbsp;元素序列。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [1,2,4,5], k = 3</span></p>
<p><span class="example-io"><b>输出:</b>1</span></p>
<p><strong>解释:</strong></p>
<p>子数组&nbsp;<code>nums[2..3]</code> 的按位&nbsp;<code>AND</code>&nbsp;运算值为 4 ,得到最小差值&nbsp;<code>|3 - 4| = 1</code>&nbsp;</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [1,2,1,2], k = 2</span></p>
<p><span class="example-io"><b>输出:</b>0</span></p>
<p><strong>解释:</strong></p>
<p>子数组&nbsp;<code>nums[1..1]</code> 的按位&nbsp;<code>AND</code>&nbsp;运算值为 2 ,得到最小差值&nbsp;<code>|2 - 2| = 0</code>&nbsp;</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [1], k = 10</span></p>
<p><span class="example-io"><b>输出:</b>9</span></p>
<p><strong>解释:</strong></p>
<p>只有一个子数组,按位&nbsp;<code>AND</code>&nbsp;运算值为 1 ,得到最小差值&nbsp;<code>|10 - 1| = 9</code>&nbsp;</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>