This file contains hidden or 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
| const sqrt = Math.sqrt; | |
| const pow = Math.pow; | |
| const fibCalc = n => Math.round( | |
| (1 / sqrt(5)) * | |
| ( | |
| pow(((1 + sqrt(5)) / 2), n) - | |
| pow(((1 - sqrt(5)) / 2), n) | |
| ) | |
| ); |
This file contains hidden or 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
| head.next.next = head; | |
| head.next = null; |
This file contains hidden or 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
| def F(n): | |
| if n == 0: return 0 | |
| elif n == 1: return 1 | |
| else: return F(n-1)+F(n-2) |
This file contains hidden or 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
| var reverseList = function(head) { | |
| if (!head) { | |
| return head; | |
| } | |
| let pre = head.next; | |
| head.next = null; | |
| return fun(head, pre); | |
| function fun(cur, pre) { | |
| if (pre == null) { |
This file contains hidden or 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
| function fib(n, a = 0, b = 1){ | |
| if (n > 0) { | |
| return fib(n - 1, b, a + b) | |
| } | |
| return a | |
| } |
This file contains hidden or 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
| φ = (1+sqrt(5))/2 |
This file contains hidden or 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
| function fib(n, a = 0, b = 1){ | |
| if (n > 0) { | |
| return fib(n - 1, b, a + b) | |
| } | |
| return a | |
| } |
This file contains hidden or 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
| def BinaryTreeToDLL(self, node): | |
| #Checks whether node is None | |
| if(node == None): | |
| return; | |
| #Convert left subtree to doubly linked list | |
| self.BinaryTreeToDLL(node.left); | |
| #If list is empty, add node as head of the list | |
| if(self.head == None): |
This file contains hidden or 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
| class Solution(object): | |
| def reverseList(self, head): # Recursive | |
| """ | |
| :type head: ListNode | |
| :rtype: ListNode | |
| """ | |
| if not head or not head.next: | |
| return head | |
| p = self.reverseList(head.next) | |
| head.next.next = head |
This file contains hidden or 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
| static double inverseSqrt5 = 1 / Math.Sqrt(5); | |
| static double phi = (1 + Math.Sqrt(5)) / 2; | |
| /* should use | |
| const double inverseSqrt5 = 0.44721359549995793928183473374626 | |
| const double phi = 1.6180339887498948482045868343656 | |
| */ | |
| static int Fibonacci(int n) { | |
| return (int)Math.Floor(Math.Pow(phi, n) * inverseSqrt5 + 0.5); | |
| } |