-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5c02a7
commit 6ea5328
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* Definition for a singly-linked list. | ||
* class ListNode { | ||
* public $val = 0; | ||
* public $next = null; | ||
* function __construct($val) { $this->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 | ||
Check failure on line 41 in index.php GitHub Actions / phpcs
|
||
{ | ||
|
||
/** | ||
* @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); | ||
} | ||
} | ||
} | ||