
LeetCode with Python
文章平均质量分 73
3x3只眼
圣骑MT,专供各类型号圣光
展开
-
【LeetCode with Python】 Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211.Given an integer n, generate the nt原创 2014-09-21 17:38:23 · 10362 阅读 · 0 评论 -
【LeetCode with Python】 Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set原创 2014-07-04 16:26:33 · 1966 阅读 · 0 评论 -
【LeetCode with Python】 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals [1,3],[6,9], insert and merge [2,5原创 2014-09-21 17:41:53 · 21460 阅读 · 0 评论 -
【LeetCode with Python】 Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?原创 2014-07-06 14:28:22 · 3756 阅读 · 0 评论 -
【LeetCode with Python】 Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a原创 2014-07-06 14:53:52 · 26650 阅读 · 0 评论 -
【LeetCode with Python】 Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note: You may only use constant extra space.For example,Given the following binary tree原创 2014-07-04 16:29:12 · 2036 阅读 · 0 评论 -
【LeetCode with Python】 Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of times.Note: All numbers (including target) will原创 2014-09-21 17:30:59 · 16477 阅读 · 1 评论 -
【LeetCode with Python】 Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Given n will alway原创 2014-07-04 16:34:20 · 2068 阅读 · 0 评论 -
【LeetCode with Python】 Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \原创 2014-09-21 17:38:47 · 10819 阅读 · 0 评论 -
【LeetCode with Python】 Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given 1->4->3->2->5->2原创 2014-09-21 17:46:32 · 10311 阅读 · 0 评论 -
【LeetCode with Python】 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2014-09-21 17:33:01 · 2857 阅读 · 0 评论 -
【LeetCode with Python】 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.原创 2014-07-06 15:07:26 · 2949 阅读 · 0 评论 -
【LeetCode with Python】 Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.原创 2014-07-06 15:01:14 · 4185 阅读 · 0 评论 -
【LeetCode with Python】 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.原创 2008-12-07 13:17:00 · 1216 阅读 · 0 评论 -
【LeetCode with Python】 Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more cand原创 2014-09-21 17:31:34 · 2951 阅读 · 0 评论 -
【LeetCode with Python】 Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].原创 2014-07-20 16:27:04 · 16195 阅读 · 0 评论 -
【LeetCode with Python】 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:原创 2014-07-06 15:34:35 · 5823 阅读 · 0 评论 -
【LeetCode with Python】 Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].原创 2008-12-07 13:55:00 · 9422 阅读 · 0 评论 -
【LeetCode with Python】 Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing原创 2014-11-30 21:52:37 · 12024 阅读 · 0 评论 -
【LeetCode with Python】 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]原创 2014-11-30 21:50:32 · 12902 阅读 · 0 评论 -
【LeetCode with Python】 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.原创 2014-09-21 17:35:33 · 2948 阅读 · 0 评论 -
【LeetCode with Python】 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.Note: All numbers (including target) will be pos原创 2014-09-21 17:33:54 · 3140 阅读 · 0 评论 -
【LeetCode with Python】 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "原创 2014-07-06 15:05:22 · 3876 阅读 · 0 评论 -
【LeetCode with Python】 Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.For example,If S = [1,2,2], a solutio原创 2014-07-04 16:11:45 · 3012 阅读 · 0 评论 -
【LeetCode with Python】 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.原创 2008-12-07 13:51:00 · 9443 阅读 · 0 评论 -
【LeetCode with Python】 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.原创 2008-09-13 00:43:00 · 10393 阅读 · 0 评论 -
【LeetCode with Python】 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return [1,2,3,6,9,8,7,4,5].原创 2014-11-30 21:49:27 · 12459 阅读 · 0 评论 -
【LeetCode with Python】 N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.原创 2014-09-21 17:45:37 · 10376 阅读 · 0 评论 -
【LeetCode with Python】 N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.Each solution contains a distinct board configuration of the原创 2014-09-21 17:44:49 · 10734 阅读 · 0 评论 -
【LeetCode with Python】 Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.原创 2014-09-21 17:35:58 · 3027 阅读 · 0 评论 -
【LeetCode with Python】 Sort List
Sort a linked list in O(n log n) time using constant space complexity.原创 2014-07-26 14:17:25 · 15616 阅读 · 0 评论 -
【LeetCode with Python】 Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?原创 2014-07-06 15:17:49 · 3018 阅读 · 0 评论 -
【LeetCode with Python】 Binary Tree Level Order Traversal
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 7return its level order traversal as:[ [3]原创 2014-07-06 14:12:36 · 2645 阅读 · 0 评论 -
【LeetCode with Python】 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?原创 2008-12-07 13:31:00 · 65733 阅读 · 0 评论 -
【LeetCode with Python】 LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key,原创 2008-09-13 00:32:00 · 1635 阅读 · 0 评论 -
【LeetCode with Python】 Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate原创 2014-09-21 17:47:15 · 10565 阅读 · 0 评论 -
【LeetCode with Python】 Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.原创 2014-07-06 15:26:46 · 6466 阅读 · 0 评论 -
【LeetCode with Python】 Insertion Sort List
Sort a linked list using insertion sort.原创 2008-12-07 13:30:00 · 2772 阅读 · 0 评论 -
【LeetCode with Python】 Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A = [1,1原创 2008-12-07 13:18:00 · 1684 阅读 · 0 评论 -
【LeetCode with Python】 String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.Notes: It is intended for this problem to be specifi原创 2008-12-07 12:58:00 · 1665 阅读 · 0 评论