检查两个数组是环状排列环状、数组、排列、两个

2023-09-11 04:01:04 作者:此釹籽yi無芯

由于两个数组,你怎么检查,如果一个是另一个的循环置换?

Given two arrays, how do you check if one is a cyclic permutation of the other?

例如,由于 A = [1,2,3,1,5] B = 3,1,5,1 ,2] C = [2,1,3,1,5] 我们有一个 A B 是环状排列,但 C 不是非此即彼的循环置换。

For example, given a = [1, 2, 3, 1, 5], b = [3, 1, 5, 1, 2], and c = [2, 1, 3, 1, 5] we have that a and b are cyclic permutations but c is not a cyclic permutation of either.

注:该阵列可有重复的元素

Note: the arrays may have duplicate elements.

推荐答案

这里的标准窍门是连接与自身的阵列之一,并试图找到第二个数组串接的数组中开始。

The standard trick here is to concatenate one of the arrays with itself, and then try to find the 2nd array in the concatenated array.

例如,'A'连接在一起本身是:

For example, 'a' concatenated with itself is:

[1,2,3,1,5,1,2,3,1,5]

[1, 2, 3, 1, 5, 1, 2, 3, 1, 5]

既然你看到从3元素数组开始'B',那么a和b是循环排列。

Since you do see 'b' in this array starting from the 3rd element then a and b are cyclic permutations.

 
精彩推荐
图片推荐