Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: добавил решения алгоритмических задач #1197

Open
wants to merge 1 commit into
base: AYamaliev/main
Choose a base branch
from
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
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

View workflow job for this annotation

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

View workflow job for this annotation

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) - по памяти`
Loading