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