|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
8 | 8 |
|
9 |
| -<p>给你一个待查数组 <code>queries</code> ,数组中的元素为 <code>1</code> 到 <code>m</code> 之间的正整数。 请你根据以下规则处理所有待查项 <code>queries[i]</code>(从 <code>i=0</code> 到 <code>i=queries.length-1</code>):</p> |
| 9 | +<p>给定一个正整数数组 <code>queries</code> ,其取值范围在 <code>1</code> 到 <code>m</code> 之间。 请你根据以下规则按顺序处理所有 <code>queries[i]</code>(从 <code>i=0</code> 到 <code>i=queries.length-1</code>):</p> |
10 | 10 |
|
11 | 11 | <ul>
|
12 |
| - <li>一开始,排列 <code>P=[1,2,3,...,m]</code>。</li> |
13 |
| - <li>对于当前的 <code>i</code> ,请你找出待查项 <code>queries[i]</code> 在排列 <code>P</code> 中的位置(<strong>下标从 0 开始</strong>),然后将其从原位置移动到排列 <code>P</code> 的起始位置(即下标为 0 处)。注意, <code>queries[i]</code> 在 <code>P</code> 中的位置就是 <code>queries[i]</code> 的查询结果。</li> |
| 12 | + <li>首先,你有一个排列 <code>P=[1,2,3,...,m]</code>。</li> |
| 13 | + <li>对于当前的 <code>i</code> ,找到 <code>queries[i]</code> 在排列 <code>P</code> 中的位置(<b>从 0 开始索引</b>),然后将它移到排列 <code>P</code> 的开头(即下标为 0 处)。注意, <code>queries[i]</code> 的查询结果是 <code>queries[i]</code> 在 <code>P</code> 中移动前的位置。</li> |
14 | 14 | </ul>
|
15 | 15 |
|
16 |
| -<p>请你以数组形式返回待查数组 <code>queries</code> 的查询结果。</p> |
| 16 | +<p>返回一个数组,包含从给定 <code>queries</code> 中查询到的结果。</p> |
17 | 17 |
|
18 | 18 | <p> </p>
|
19 | 19 |
|
20 | 20 | <p><strong>示例 1:</strong></p>
|
21 | 21 |
|
22 |
| -<pre><strong>输入:</strong>queries = [3,1,2,1], m = 5 |
| 22 | +<pre> |
| 23 | +<strong>输入:</strong>queries = [3,1,2,1], m = 5 |
23 | 24 | <strong>输出:</strong>[2,1,2,1]
|
24 |
| -<strong>解释:</strong>待查数组 queries 处理如下: |
25 |
| -对于 i=0: queries[i]=3, P=[1,2,3,4,5], 3 在 P 中的位置是 <strong>2</strong>,接着我们把 3 移动到 P 的起始位置,得到 P=[3,1,2,4,5] 。 |
26 |
| -对于 i=1: queries[i]=1, P=[3,1,2,4,5], 1 在 P 中的位置是 <strong>1</strong>,接着我们把 1 移动到 P 的起始位置,得到 P=[1,3,2,4,5] 。 |
27 |
| -对于 i=2: queries[i]=2, P=[1,3,2,4,5], 2 在 P 中的位置是 <strong>2</strong>,接着我们把 2 移动到 P 的起始位置,得到 P=[2,1,3,4,5] 。 |
28 |
| -对于 i=3: queries[i]=1, P=[2,1,3,4,5], 1 在 P 中的位置是 <strong>1</strong>,接着我们把 1 移动到 P 的起始位置,得到 P=[1,2,3,4,5] 。 |
29 |
| -因此,返回的结果数组为 [2,1,2,1] 。 |
| 25 | +<strong>解释:处理</strong> queries 的过程如下: |
| 26 | +对于 i=0: queries[i]=3, P=[1,2,3,4,5], 3 在 P 中的位置是 <strong>2</strong>,然后我们把 3 移动到 P 的开头,得到 P=[3,1,2,4,5] 。 |
| 27 | +对于 i=1: queries[i]=1, P=[3,1,2,4,5], 1 在 P 中的位置是 <strong>1</strong>,然后我们把 1 移动到 P 的开头,得到 P=[1,3,2,4,5] 。 |
| 28 | +对于 i=2: queries[i]=2, P=[1,3,2,4,5], 2 在 P 中的位置是 <strong>2</strong>,然后我们把 2 移动到 P 的开头,得到 P=[2,1,3,4,5] 。 |
| 29 | +对于 i=3: queries[i]=1, P=[2,1,3,4,5], 1 在 P 中的位置是 <strong>1</strong>,然后我们把 1 移动到 P 的开头,得到 P=[1,2,3,4,5] 。 |
| 30 | +因此,包含结果的数组为 [2,1,2,1] 。 |
30 | 31 | </pre>
|
31 | 32 |
|
32 | 33 | <p><strong>示例 2:</strong></p>
|
33 | 34 |
|
34 |
| -<pre><strong>输入:</strong>queries = [4,1,2,2], m = 4 |
| 35 | +<pre> |
| 36 | +<strong>输入:</strong>queries = [4,1,2,2], m = 4 |
35 | 37 | <strong>输出:</strong>[3,1,2,0]
|
36 | 38 | </pre>
|
37 | 39 |
|
38 | 40 | <p><strong>示例 3:</strong></p>
|
39 | 41 |
|
40 |
| -<pre><strong>输入:</strong>queries = [7,5,5,8,3], m = 8 |
| 42 | +<pre> |
| 43 | +<strong>输入:</strong>queries = [7,5,5,8,3], m = 8 |
41 | 44 | <strong>输出:</strong>[6,5,0,7,5]
|
42 | 45 | </pre>
|
43 | 46 |
|
|
0 commit comments