From 6ea53285e1530343f08fc148dc261f184d6d6f3d Mon Sep 17 00:00:00 2001 From: vasilaki-t Date: Sun, 10 Mar 2024 16:19:24 +0500 Subject: [PATCH] HW14 --- index.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 000000000..2b5db975e --- /dev/null +++ b/index.php @@ -0,0 +1,87 @@ +val = $val; } + * } + */ + +class Solution +{ + /** + * @param ListNode $head + * @return Boolean + */ + function hasCycle($head) + { + if ($head == null || $head->next == null) { + return false; + } + + $slow = $head; + $fast = $head->next; + + while ($slow != $fast) { + + if ($fast == null || $fast->next == null) { + return false; + } + + $slow = $slow->next; + $fast = $fast->next->next; + } + + return true; + } +} + + +class Solution2 +{ + + /** + * @param String $digits + * @return String[] + */ + function letterCombinations($digits) + { + if (empty($digits)) { + return []; // возвращаем пустой массив для пустой строки ввода + } + + $digitMap = [ + '2' => ['a', 'b', 'c'], + '3' => ['d', 'e', 'f'], + '4' => ['g', 'h', 'i'], + '5' => ['j', 'k', 'l'], + '6' => ['m', 'n', 'o'], + '7' => ['p', 'q', 'r', 's'], + '8' => ['t', 'u', 'v'], + '9' => ['w', 'x', 'y', 'z'], + ]; + + $result = []; + $currentCombination = ''; + + $this->generateCombinations($digits, 0, $digitMap, $currentCombination, $result); + + return $result; + } + + function generateCombinations($digits, $index, $digitMap, $currentCombination, &$result) + { + if ($index == strlen($digits)) { + $result[] = $currentCombination; + return; + } + + $currentDigit = $digits[$index]; + $letters = $digitMap[$currentDigit]; + + foreach ($letters as $letter) { + $this->generateCombinations($digits, $index + 1, $digitMap, $currentCombination . $letter, $result); + } + } +} \ No newline at end of file