
LeetCode with Python
文章平均质量分 70
用python解决LeetCode.com的题目。目前关于LeetCode题目解析的文章虽多,但都是用C++和Java解决,鲜有用Python来进行描述的,本专栏可以弥补这方面文章的缺失。
3x3只眼
圣骑MT,专供各类型号圣光
展开
-
-
-原创 2014-11-30 21:48:12 · 12261 阅读 · 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】 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】 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】 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】 Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the di原创 2014-08-29 18:11:24 · 3357 阅读 · 0 评论 -
【LeetCode with Python】 Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.原创 2014-08-29 18:34:04 · 3126 阅读 · 0 评论 -
-
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?原创 2014-08-29 18:35:11 · 13253 阅读 · 0 评论 -
【LeetCode with Python】 Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.原创 2014-08-29 18:36:24 · 3327 阅读 · 0 评论 -
【LeetCode with Python】 Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3].原创 2014-08-29 18:37:22 · 3019 阅读 · 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】 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】 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】 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】 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】 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】 Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.原创 2014-09-21 17:37:42 · 10028 阅读 · 0 评论 -
【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】 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】 Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.原创 2014-09-21 17:41:21 · 10734 阅读 · 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】 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】 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】 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】 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 评论 -
-
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3].原创 2014-09-21 17:47:40 · 16198 阅读 · 0 评论 -
【LeetCode with Python】 Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases: Did you consider the case where path = "/../"? In this case, you sh原创 2014-09-21 17:49:20 · 12680 阅读 · 0 评论 -
【LeetCode with Python】 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1原创 2014-09-21 17:50:11 · 111859 阅读 · 0 评论 -
【LeetCode with Python】 Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue re原创 2014-09-21 17:51:15 · 14037 阅读 · 0 评论 -
【LeetCode with Python】 Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?原创 2014-09-21 17:52:05 · 122344 阅读 · 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】 Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?原创 2014-07-19 21:04:39 · 4000 阅读 · 0 评论 -
【LeetCode with Python】 Subsets
Given a set of distinct integers, 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,3], a solution is:[ [3], [1], [2]原创 2014-07-04 16:04:01 · 2522 阅读 · 0 评论 -
【LeetCode with Python】 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"原创 2008-09-13 00:20:00 · 2138 阅读 · 0 评论 -
【LeetCode with Python】 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.原创 2008-09-13 00:23:00 · 9982 阅读 · 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】 Permutations
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].原创 2008-09-13 00:39:00 · 10514 阅读 · 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】 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:b原创 2008-09-13 00:46:00 · 1371 阅读 · 0 评论 -
【LeetCode with Python】 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prot原创 2008-09-13 10:03:00 · 1789 阅读 · 0 评论