Description
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules:
- In the beginning, you have the permutation P=[1,2,3,…,m].
- For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].
Return an array containing the result for the given queries.
Example 1:
Example 2:
Example 3:
Constraints:
- 1 <= m <= 10^3
- 1 <= queries.length <= m
- 1 <= queries[i] <= m
分析
题目的意思是:给你queries,代表着排列中的数,返回其对应的数字,在每次query后,需要把当前位置的数字放到最开始的位置,然后其他位置的数依次移动到后面。
所以我的思路也很直接,先把这个排列表示出来,用P表示,按照query的顺序每次排列P,即把找到的数提前,其他的移动到后面去。
代码