Rumah > Soal Jawab > teks badan
P粉1116277872023-09-04 00:34:22
Tiada fungsi terbina dalam, tetapi anda boleh memutar setiap kedudukan dan membandingkan jika ia betul.
$isRotated = function (array $original, array $maybeRotated): bool { $originalCount = count($original); $maybeRotatedCount = count($maybeRotated); if ($originalCount !== $maybeRotatedCount || $original === $maybeRotated) { return false; } for ($i = 0; $i < $originalCount; $i++) { $original[] = array_shift($original); if ($original === $maybeRotated) { return true; } } return false; }; echo $isRotated([1, 2, 3, 4, 5, 6, 7, 8], [6, 7, 8, 1, 2, 3, 4, 5]) ? 'true' : 'false', PHP_EOL; echo $isRotated([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8]) ? 'true' : 'false', PHP_EOL; echo $isRotated([1, 2, 3, 4, 5, 6, 7, 8], [2, 3, 4, 5, 6, 7, 8, 1]) ? 'true' : 'false', PHP_EOL;
true false true