Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# PHP_2026
# Задание 18: IS/hw18

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus
## Общее описание

```text
Leetcode практикум
```

## Алгоритмическая сложность

### 160. Intersection of Two Linked Lists

```text
leetcode_160.php
Сложность в худшем случае O(n*m), т.к происходит полный перебор списка B на каждый элемент списка A, где n длинна списка A, m длинна списка B.
```

### 160. Intersection of Two Linked Lists (вариант с двумя указателями)

```text
leetcode_160_2.php
Сложность O(n+m) и O(1), два указателя идут по спискам, при достижении конца переходят на начало другого - сходятся в точке пересечения ©.
```

### 166. Fraction to Recurring Decimal

```text
leetcode_166.php
Сложность O(1), т.к все операции имеют сложность O(1).
```
83 changes: 83 additions & 0 deletions leetcode_160.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

class ListNode
{
public int $val = 0;
public ?ListNode $next = null;
function __construct(int $val)
{
$this->val = $val;
}
}


class Solution
{
/**
* @param ListNode $headA
* @param ListNode $headB
* @return ListNode
*/
function getIntersectionNode($headA, $headB): ?ListNode
{
if (is_null($headA)) return null;
$headBCopy = $headB;
while (!is_null($headBCopy)) {
if ($headA === $headBCopy) return $headA;
$headBCopy = $headBCopy->next;
}

return $this->getIntersectionNode($headA->next, $headB);
}
}

$solution = new Solution;
// example 1
$intersectionList = new ListNode(8);
$intersectionList->next = new ListNode(4);
$intersectionList->next->next = new ListNode(5);

// $listA = [4, 1, 8, 4, 5];
$headA = new ListNode(4);
$headA->next = new ListNode(1);
$headA->next->next = $intersectionList;

// $listB = [5, 6, 1, 8, 4, 5];
$headB = new ListNode(5);
$headB->next = new ListNode(6);
$headB->next->next = new ListNode(1);
$headB->next->next->next = $intersectionList;

$intersected = $solution->getIntersectionNode($headA, $headB);
echo ($intersected->val ?? 'No intersection') . PHP_EOL;

// example 2
$intersectionList = new ListNode(2);
$intersectionList->next = new ListNode(4);

// $listA = [1,9,1,2,4];
$headA = new ListNode(1);
$headA->next = new ListNode(9);
$headA->next->next = new ListNode(1);
$headA->next->next->next = $intersectionList;

// listB = [3,2,4]
$headB = new ListNode(3);
$headB->next = $intersectionList;

$intersected = $solution->getIntersectionNode($headA, $headB);
echo ($intersected->val ?? 'No intersection') . PHP_EOL;


// example 3
// listA = [2,6,4]
$headA = new ListNode(2);
$headA->next = new ListNode(6);
$headA->next->next = new ListNode(4);

// listB = [1,5]
$headB = new ListNode(1);
$headB->next = new ListNode(6);

$intersected = $solution->getIntersectionNode($headA, $headB);
echo ($intersected->val ?? 'No intersection') . PHP_EOL;
85 changes: 85 additions & 0 deletions leetcode_160_2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

class ListNode
{
public int $val = 0;
public ?ListNode $next = null;
function __construct(int $val)
{
$this->val = $val;
}
}


class Solution
{
/**
* @param ListNode $headA
* @param ListNode $headB
* @return ListNode
*/
function getIntersectionNode($headA, $headB): ?ListNode
{
$currentA = $headA;
$currentB = $headB;

while ($currentA !== $currentB) {
$currentA = $currentA == null ? $headB : $currentA->next;
$currentB = $currentB == null ? $headA : $currentB->next;
}

return $currentA;
}
}

$solution = new Solution;
// example 1
$intersectionList = new ListNode(8);
$intersectionList->next = new ListNode(4);
$intersectionList->next->next = new ListNode(5);

// $listA = [4, 1, 8, 4, 5];
$headA = new ListNode(4);
$headA->next = new ListNode(1);
$headA->next->next = $intersectionList;

// $listB = [5, 6, 1, 8, 4, 5];
$headB = new ListNode(5);
$headB->next = new ListNode(6);
$headB->next->next = new ListNode(1);
$headB->next->next->next = $intersectionList;

$intersected = $solution->getIntersectionNode($headA, $headB);
echo ($intersected->val ?? 'No intersection') . PHP_EOL;

// example 2
$intersectionList = new ListNode(2);
$intersectionList->next = new ListNode(4);

// $listA = [1,9,1,2,4];
$headA = new ListNode(1);
$headA->next = new ListNode(9);
$headA->next->next = new ListNode(1);
$headA->next->next->next = $intersectionList;

// listB = [3,2,4]
$headB = new ListNode(3);
$headB->next = $intersectionList;

$intersected = $solution->getIntersectionNode($headA, $headB);
echo ($intersected->val ?? 'No intersection') . PHP_EOL;


// example 3
// listA = [2,6,4]
$headA = new ListNode(2);
$headA->next = new ListNode(6);
$headA->next->next = new ListNode(4);

// listB = [1,5]
$headB = new ListNode(1);
$headB->next = new ListNode(5);
$headA->next->next = new ListNode(7);

$intersected = $solution->getIntersectionNode($headA, $headB);
echo ($intersected->val ?? 'No intersection') . PHP_EOL;
92 changes: 92 additions & 0 deletions leetcode_166.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

class Solution
{

/**
* @param int $numerator
* @param int $denominator
* @return String
*/
function fractionToDecimal(int $numerator, int $denominator)
{
$result = '';

// Проверяем будет ли результат отрицательным и если да, то ставим в начале минус и значения берем по модулю
if (($numerator > 0 && $denominator < 0) || ($numerator < 0 && $denominator > 0)) {
$result = '-';
$numerator = abs($numerator);
$denominator = abs($denominator);
}

// Получаем целую часть
$result .= intdiv($numerator, $denominator);

// Если остатка от отделения нет, то возвращаем результат
if (($numerator % $denominator) === 0) {
return $result;
}

$result .= '.';
$hash = [];
$numerator = $numerator % $denominator;
while (true) {
$numerator = (int)($numerator * 10);

// Если делитель начинает повторяться, записываем дробь с периодом и выходим из цикла
if (isset($hash[$numerator])) {
$result .= '(' . implode('', $hash) . ')';
break;
}
$hash[$numerator] = intdiv($numerator, $denominator);
$numerator = $numerator % $denominator;

// Если деление закончилось без остаткка, то записываем результат в дробную часть и выходим из цикла
if ($numerator === 0) {
$result .= implode('', $hash);
break;
}
}

return $result;
}
}

$solution = new Solution;
// Example 1:
// Input: numerator = 1, denominator = 2
$result = $solution->fractionToDecimal(1, 2);
echo $result . PHP_EOL;
// Output: "0.5"


// Example 2:
// Input: numerator = 2, denominator = 1
$result = $solution->fractionToDecimal(2, 1);
echo $result . PHP_EOL;
// Output: "2"


// Example 3:
// Input: numerator = 4, denominator = 333
$result = $solution->fractionToDecimal(4, 333);
echo $result . PHP_EOL;
// Output: "0.(012)"

// Example 4:
// Input: numerator = 22, denominator = 7
$result = $solution->fractionToDecimal(22, 7);
echo $result . PHP_EOL;
// Output: "3.(142857)"

// Example 5:
// Input: numerator = -1, denominator = -2
$result = $solution->fractionToDecimal(-1, -2);
echo $result . PHP_EOL;
// Output: "0.5"

// Example 6:
// Input: numerator = 1, denominator = 97
$result = $solution->fractionToDecimal(1, 97);
echo $result . PHP_EOL;
// Output: "0.5"
Loading