Skip to content

Commit

Permalink
feat: добавил решения алгоритмических задач
Browse files Browse the repository at this point in the history
Artyrcha committed Apr 6, 2024
1 parent f5c02a7 commit 51369ec
Showing 4 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
/app/.env
49 changes: 49 additions & 0 deletions app/FractiontoRecurringDecimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

class Solution

Check failure on line 5 in app/FractiontoRecurringDecimal.php

GitHub Actions / phpcs

Each class must be in a namespace of at least one level (a top-level vendor name)
{
/**
* @param Integer $numerator
* @param Integer $denominator
* @return String
*/
public function fractionToDecimal($numerator, $denominator)
{
if ($numerator == 0) {
return "0";
}

$output = '';

if (($numerator < 0 && $denominator > 0) || ($numerator > 0 && $denominator < 0)) {
$output .= '-';
}

$numerator = abs($numerator);
$denominator = abs($denominator);
$output .= intval($numerator / $denominator);

if ($numerator % $denominator != 0) {
$output .= '.';
}

$remainder = $numerator % $denominator;
$hash = [];

while ($remainder != 0 && !isset($hash[$remainder])) {
$hash[$remainder] = strlen($output);
$remainder *= 10;
$output .= intval($remainder / $denominator);
$remainder %= $denominator;
}

if ($remainder == 0) {
return $output;
} else {
$index = $hash[$remainder];
return substr($output, 0, $index) . '(' . substr($output, $index) . ')';
}
}
}
36 changes: 36 additions & 0 deletions app/IntersectionofTwoLinkedLists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val) { $this->val = $val; }
* }
*/
class Solution

Check failure on line 13 in app/IntersectionofTwoLinkedLists.php

GitHub Actions / phpcs

Each class must be in a namespace of at least one level (a top-level vendor name)
{
/**
* @param ListNode $headA
* @param ListNode $headB
* @return ListNode
*/
public function getIntersectionNode($headA, $headB)
{
if ($headA == null || $headB == null) {
return null;
}

$nodeA = $headA;
$nodeB = $headB;

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

return $nodeA;
}
}
27 changes: 27 additions & 0 deletions app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Homework 18

## 160. Intersection of Two Linked Lists

https://leetcode.com/problems/intersection-of-two-linked-lists/description/

### Ссылка на решение:

https://leetcode.com/problems/intersection-of-two-linked-lists/submissions/1224708498/

### Сложность

1. `O(m+n) - по времени`
2. `O(1) - по памяти`

## 166. Fraction to Recurring Decimal

https://leetcode.com/problems/fraction-to-recurring-decimal/description/

### Ссылка на решение:

https://leetcode.com/problems/fraction-to-recurring-decimal/submissions/1224709679/

### Сложность

1. `O(m) - по времени`
2. `O(m) - по памяти`

0 comments on commit 51369ec

Please sign in to comment.