-
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.
feat: добавил решения алгоритмических задач
Showing
4 changed files
with
114 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,2 @@ | ||
.idea/ | ||
/app/.env |
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class Solution | ||
{ | ||
/** | ||
* @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) . ')'; | ||
} | ||
} | ||
} |
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,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 | ||
{ | ||
/** | ||
* @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; | ||
} | ||
} |
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,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) - по памяти` |