Pow(x, n) Total Accepted: 67450 Total Submissions: 250035 Difficulty: Medium Implement pow(x, n). https://leetcode.com/problems/powx-n/ Code class Solution(object): def myPow(self, x, n): “”” :type x: float :type n: int :rtype: float “”” if n==0: return 1 tmp = self.myPow(x, abs(n)/2) tmp = tmp * x * tmp if n%2 else tmp * tmp return tmp …
Author Archives: czxttkl
Leetcode 142: Linked List Cycle II
https://leetcode.com/problems/linked-list-cycle-ii/ Linked List Cycle II Total Accepted: 56706 Total Submissions: 180315 Difficulty: Medium Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? Idea As the plot shows, we have …
Leetcode 215: Kth Largest Element in an Array
https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Total Accepted: 25004 Total Submissions: 87854 Difficulty: Medium Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k …
Continue reading “Leetcode 215: Kth Largest Element in an Array”
Leetcode 102: Binary Tree Level Order Traversal
https://leetcode.com/problems/binary-tree-level-order-traversal/ Binary Tree Level Order Traversal Total Accepted: 70821 Total Submissions: 237732 Difficulty: Easy Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: …
Continue reading “Leetcode 102: Binary Tree Level Order Traversal”
Leetcode 107: Binary Tree Level Order Traversal II
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Binary Tree Level Order Traversal II Total Accepted: 56136 Total Submissions: 177858 Difficulty: Easy Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 …
Continue reading “Leetcode 107: Binary Tree Level Order Traversal II”
Solve Travelling Salesman Problem using DP
Claim In this post, I am using the same notation as in the video below and the wiki page (https://en.wikipedia.org/wiki/Held%E2%80%93Karp_algorithm) Our problem is given a matrix costs in which costs[i][j] is the traveling cost going from node i to node j, we are asked to return the minimum cost for traveling from node 0 and visit …
Continue reading “Solve Travelling Salesman Problem using DP”
Leetcode 156: Binary Tree Upside Down
https://leetcode.com/problems/binary-tree-upside-down/ Binary Tree Upside Down Total Accepted: 4861 Total Submissions: 13934 Difficulty: Medium Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes …
Leetcode 145: Binary Tree Postorder Traversal
https://leetcode.com/problems/binary-tree-postorder-traversal/ Binary Tree Postorder Traversal Total Accepted: 76404 Total Submissions: 229965 Difficulty: Hard Given a binary tree, return the postorder traversal of its nodes’ values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? Idea: This problem is similar to a …
Continue reading “Leetcode 145: Binary Tree Postorder Traversal”
Leetcode 144: Binary Tree Preorder Traversal
https://leetcode.com/discuss/23326/very-simple-iterative-python-solution Binary Tree Preorder Traversal Total Accepted: 89130 Total Submissions: 240451 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes’ values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Code # Definition for a binary tree …
Continue reading “Leetcode 144: Binary Tree Preorder Traversal”
Leetcode 235: Lowest Common Ancestor of a Binary Search Tree (BST)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ Lowest Common Ancestor of a Binary Search Tree Total Accepted: 30832 Total Submissions: 81197 Difficulty: Easy Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and …
Continue reading “Leetcode 235: Lowest Common Ancestor of a Binary Search Tree (BST)”