https://leetcode.com/problems/permutations/ Permutations Total Accepted: 70053 Total Submissions: 214523 Difficulty: Medium Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. Code class Solution(object): def permute(self, nums): “”” :type nums: List[int] :rtype: List[List[int]] “”” if not nums: return [] res = [] self.recur(nums, 0, …
Author Archives: czxttkl
Leetcode 99: Recover Binary Search Tree
https://leetcode.com/problems/recover-binary-search-tree/ Recover Binary Search Tree Total Accepted: 41308 Total Submissions: 166942 Difficulty: Hard Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}" means? > read more …
Leetcode 239: Sliding Window Maximum
https://leetcode.com/problems/sliding-window-maximum/ Sliding Window Maximum Total Accepted: 13329 Total Submissions: 57131 Difficulty: Hard Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right …
Find Kth Smallest Element in Two Sorted Arrays
Problem Given K and two sorted arrays with length M and N, find the K-th smallest element among all elements in the two arrays. Code import sys class Solution(object): def findKthSmallest(self, nums1, nums2, k): “”” :type nums1: sorted List[int] :type nusm2: sorted List[int] :type k: int :rtype: int “”” if not (0 <k <= …
Continue reading “Find Kth Smallest Element in Two Sorted Arrays”
Leetcode 43: Multiply Strings
https://leetcode.com/problems/multiply-strings/ Multiply Strings Total Accepted: 43669 Total Submissions: 203735 Difficulty: Medium Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. It is not a very funny problem that can test you in the interview. Refer Idea here: https://leetcode.com/discuss/33951/ac-solution-in-java-with-explanation Refer …
Leetcode 50: Pow(x,n)
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 …
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”