检查两个数组是环状排列 [英] Check if two arrays are cyclic permutations

查看:127
本文介绍了检查两个数组是环状排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于检查两个数组是环状排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆