From 51369ec7d3628e3a2f4a18cb43c3e163de8bdc9b Mon Sep 17 00:00:00 2001 From: Artyrcha Date: Sat, 6 Apr 2024 15:11:18 +0500 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B0?= =?UTF-8?q?=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=B8=D1=85=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ app/FractiontoRecurringDecimal.php | 49 ++++++++++++++++++++++++++++ app/IntersectionofTwoLinkedLists.php | 36 ++++++++++++++++++++ app/README.md | 27 +++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 app/FractiontoRecurringDecimal.php create mode 100644 app/IntersectionofTwoLinkedLists.php create mode 100644 app/README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..f3f649d28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +/app/.env \ No newline at end of file diff --git a/app/FractiontoRecurringDecimal.php b/app/FractiontoRecurringDecimal.php new file mode 100644 index 000000000..e026bc86e --- /dev/null +++ b/app/FractiontoRecurringDecimal.php @@ -0,0 +1,49 @@ + 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) . ')'; + } + } +} diff --git a/app/IntersectionofTwoLinkedLists.php b/app/IntersectionofTwoLinkedLists.php new file mode 100644 index 000000000..3b56f0e57 --- /dev/null +++ b/app/IntersectionofTwoLinkedLists.php @@ -0,0 +1,36 @@ +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; + } +} diff --git a/app/README.md b/app/README.md new file mode 100644 index 000000000..a518c18c0 --- /dev/null +++ b/app/README.md @@ -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) - по памяти` \ No newline at end of file