leetcode算法详解
主要介绍leetcode网站里的算法题的详细解题过程。每一道题都有题意分析,思路详解和复杂度分析
zhsenl
这个作者很懒,什么都没留下…
展开
-
Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My SubmissionsGiven inorder and postorder traversal of a tree, construct the bina原创 2014-09-11 07:12:22 · 788 阅读 · 0 评论 -
Leetcode dfs&dp Triangle
Triangle Total Accepted: 17536 Total Submissions: 65508My SubmissionsGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row原创 2014-09-11 07:12:15 · 967 阅读 · 0 评论 -
Leetcode dfs Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Total Accepted: 14824 Total Submissions: 55882My SubmissionsGiven preorder and inorder traversal of a tree, construct the binary原创 2014-09-10 13:16:10 · 1046 阅读 · 0 评论 -
Leetcode dfs N-Queens II
N-Queens II Total Accepted: 12668 Total Submissions: 38109My SubmissionsFollow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct原创 2014-09-10 09:11:01 · 945 阅读 · 0 评论 -
Leetcode dfs N-Queens
N-Queens Total Accepted: 14054 Total Submissions: 54127My SubmissionsThe n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other原创 2014-09-10 09:04:43 · 998 阅读 · 0 评论 -
Leetcode dfs Path SumII
Path Sum II Total Accepted: 18489 Total Submissions: 68323My SubmissionsGiven a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For exampl原创 2014-09-10 00:37:10 · 1305 阅读 · 0 评论 -
Leetcode 贪心 Jump Game II
Jump Game II Total Accepted: 16242 Total Submissions: 65802My SubmissionsGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each elem原创 2014-09-10 00:33:08 · 1039 阅读 · 0 评论 -
Leetcode dfs Word Break II
Word Break II Total Accepted: 15138 Total Submissions: 92228My SubmissionsGiven a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a va原创 2014-09-09 23:33:51 · 1015 阅读 · 0 评论 -
Leetcode dp Word Break
Word Break Total Accepted: 22281 Total Submissions: 105657My SubmissionsGiven a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence o原创 2014-09-09 20:23:35 · 1022 阅读 · 0 评论 -
Leetcode 贪心 Jump Game
Jump Game Total Accepted: 18745 Total Submissions: 68916My SubmissionsGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each element原创 2014-09-09 10:08:31 · 761 阅读 · 0 评论 -
Leetcode dfs Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Total Accepted: 15964 Total Submissions: 60700My SubmissionsGiven a digit string, return all possible letter combinations that the number could repr原创 2014-09-08 17:08:13 · 793 阅读 · 0 评论 -
Leetcode 细节实现 Longest Common Prefix
Longest Common Prefix Total Accepted: 17298 Total Submissions: 63704My SubmissionsWrite a function to find the longest common prefix string amongst an array of strings.题意原创 2014-09-08 16:20:17 · 669 阅读 · 0 评论 -
Leetcode 栈 Longest Valid Parentheses
Longest Valid Parentheses Total Accepted: 14818 Total Submissions: 75749My SubmissionsGiven a string containing just the characters '(' and ')', find the length of the longest valid (w原创 2014-09-08 15:59:12 · 675 阅读 · 0 评论 -
Leetcode 贪心 Longest Consecutive Sequence
Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My SubmissionsGiven an unsorted array of integers, find the length of the longest consecutive elements sequence原创 2014-09-07 20:24:09 · 646 阅读 · 0 评论 -
Leetcode 模拟 Count and Say
Count and Say Total Accepted: 14508 Total Submissions: 53213My SubmissionsThe count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is原创 2014-09-07 19:44:37 · 679 阅读 · 0 评论 -
Leetcode 细节实现 Length of Last Word
Length of Last Word Total Accepted: 17518 Total Submissions: 59526My SubmissionsGiven a string s consists of upper/lower-case alphabets and empty space characters ' ', return the lengt原创 2014-09-07 15:56:19 · 759 阅读 · 0 评论 -
Leetcode 栈 Valid Parentheses
Valid Parentheses Total Accepted: 17916 Total Submissions: 63131My SubmissionsGiven a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input stri原创 2014-09-07 15:23:39 · 773 阅读 · 0 评论 -
Leetcode dfs Sudoku Solver
Sudoku Solver Total Accepted: 11799 Total Submissions: 56732My SubmissionsWrite a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character原创 2014-09-07 13:05:31 · 787 阅读 · 0 评论 -
Leetcode 细节实现 Valid Sudoku
Valid Sudoku Total Accepted: 13142 Total Submissions: 47070My SubmissionsDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially fi原创 2014-09-07 13:05:03 · 672 阅读 · 0 评论 -
Leetcode 二分查找 Search a 2D Matrix
Search a 2D Matrix Total Accepted: 18506 Total Submissions: 59402My SubmissionsWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following pr原创 2014-09-05 16:26:00 · 829 阅读 · 0 评论 -
Leetcode bfs&dfs Binary Tree Postorder Traversal II
Binary Tree Level Order Traversal II Total Accepted: 16983 Total Submissions: 54229My SubmissionsGiven a binary tree, return the bottom-up level order traversal of its nodes' values. (ie原创 2014-09-05 16:21:41 · 671 阅读 · 0 评论 -
Leetcode dfs Binary Tree Postorder Traversal
Binary Tree Postorder Traversal Total Accepted: 28560 Total Submissions: 92333My SubmissionsGiven a binary tree, return the postorder traversal of its nodes' values.For example:Given原创 2014-09-05 16:04:00 · 716 阅读 · 0 评论 -
Leetcode dfs Sum Root to Leaf Numbers
Sum Root to Leaf Numbers Total Accepted: 20237 Total Submissions: 67979My SubmissionsGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numbe原创 2014-09-05 15:38:32 · 718 阅读 · 0 评论 -
Leetcode dfs Combination SumII
Combination Sum II Total Accepted: 13710 Total Submissions: 55908My SubmissionsGiven a collection of candidate numbers (C) and a target number (T), find all unique combinations in C wher原创 2014-09-01 13:33:46 · 778 阅读 · 0 评论 -
Leetcode dfs Combination Sum
Combination Sum Total Accepted: 17319 Total Submissions: 65259My SubmissionsGiven a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the ca原创 2014-09-01 10:41:33 · 836 阅读 · 0 评论 -
Leetcode dfs Combinations
Combinations Total Accepted: 18327 Total Submissions: 60479My SubmissionsGiven two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4原创 2014-09-01 00:49:57 · 968 阅读 · 0 评论 -
Leetcode dfs Path Sum
Path Sum Total Accepted: 20393 Total Submissions: 66674My SubmissionsGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along原创 2014-08-31 20:13:02 · 942 阅读 · 0 评论 -
Leetcode 细节实现题 Spiral Matrix II
Spiral Matrix II Total Accepted: 12773 Total Submissions: 41526My SubmissionsGiven an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For exampl原创 2014-08-31 17:03:27 · 685 阅读 · 0 评论 -
Leetcode 细节实现题 Spiral Matrix
Spiral Matrix Total Accepted: 12721 Total Submissions: 62094My SubmissionsGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For ex原创 2014-08-31 16:29:30 · 694 阅读 · 0 评论 -
Leetcode 链表 Linked List Cycle II
题意:给定一个单链表,判断该链表中是否存在环,如果存在,返回环开始的节点思路:1.定义两个指针,快指针fast每次走两步,慢指针s每次走一次,如果它们在非尾结点处相遇,则说明存在环2.若存在环,设环的周长为r,相遇时,慢指针走了 slow步,快指针走了 2s步,快指针在环内已经走了 n环,则有等式 2s = s + nr => s = nr3.在相遇的时候,另设一个每次走一步的慢指针slow2从链表开头往前走。因为 s = nr,所以两个慢指针会在环的开始点相遇复杂度:时间O(n)struct原创 2014-08-30 23:04:02 · 685 阅读 · 0 评论 -
Leetcode 贪心 container with most water
题意:二维坐标系里有 n 个点 (i, ai), ai >= 0,从 (i, ai)到(i, 0)划竖线,共有 n 条竖线。找出两条竖线,使得它们构成的矩形的面积最大,矩形的高取决于最短的竖线。思路:贪心从首尾两个下标head 和trail 处开始扫描,用一个变量 maxArea 保持当前最大的矩形面积。如果head 指向的竖线短于 trail 的,则右移 head否则左移 trail计算面积,更新 maxArea复杂度:时间O(n),空间O(1)int maxArea(vector<in原创 2014-08-30 16:00:14 · 828 阅读 · 0 评论 -
Leetcode 细节实现 Rotate Image
题意:给定一个 n * n 的二维图像,将该图像顺时针旋转 90 度思路:先沿副对角线翻转一次,再沿水平中线翻转一次复杂度:时间O(n^2),空间O(1)void rotate(vector<vector<int> > &matrix){ int n = matrix.size(); //沿副对角线翻转 for(int i = 0; i < n; ++i){ for(int j = 0; j < n - i; ++j){ int i2 = n - 1 - j, j2 = n -原创 2014-08-30 15:29:41 · 715 阅读 · 0 评论 -
Leetcode 动态规划 Minimum Path Sum
题意:给定一个 m * n 的网格,网格中有非负的数字。一个机器人要从左上角走到右下角,每次只能向下或向右移动一个位置,找出一条总和最小的路径,返回最小值思路1:记忆化搜索使用一个两维 minPathSums[i][j]记录 (i,j)到(m,n)的总和最小的路径的值然后使用dfs 枚举复杂度:时间O(2^n) 空间O(n)思路2:dp设置状态为f[i][j],表示到达网格(i,j)的总和最小的路径的值,则状态转移方程为f[i][j] = min(f[i - 1][j] + f[i][原创 2014-08-30 13:14:18 · 782 阅读 · 1 评论 -
Leetcode 动态规划 Unique Paths II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlieUnique Paths II Total Accepted: 13655 Total Submissions: 49081My SubmissionsFollow up for "Unique Paths":Now consider i原创 2014-08-29 14:46:47 · 689 阅读 · 0 评论 -
Leetcode 动态规划 Unique Paths
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlieUnique Paths Total Accepted: 17915 Total Submissions: 57061My SubmissionsA robot is located at the top-left corner of a m原创 2014-08-29 11:23:49 · 832 阅读 · 0 评论 -
Leetcode 高精度 Plus One
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenliePlus One Total Accepted: 17614 Total Submissions: 55852My SubmissionsGiven a non-negative number represented as an array原创 2014-08-29 01:58:01 · 1085 阅读 · 3 评论 -
Leetcode 动态规划 Candy
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlieCandy Total Accepted: 16494 Total Submissions: 87468My SubmissionsThere are N children standing in a line. Each child is原创 2014-08-29 01:28:36 · 1561 阅读 · 0 评论 -
Leetcode 动态规划 Trapping Rain Water
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlieTrapping Rain Water Total Accepted: 14568 Total Submissions: 50810My SubmissionsGiven n non-negative integers representin原创 2014-08-29 01:15:02 · 1260 阅读 · 0 评论 -
Leetcode 动态规划 Maximum Subarray
题意:最大连续子序列和,在一个数组中找到和最大的连续子数组思路:dp, 对于第i个数,有两种选择:把它加入在子数组里,不加入子数组(子数组到此结束)加不加入子数组,要比较它加入前后子数组的总和是变大了还是变小了,如果变大则加入,变小则不加入所以,我们需要记录以i-1结尾的子数组的总和,最后的结果在这些总和中取最大的那个f[i] = max(f[i-1]+a[i],f[i-1]);max({f[i]})实现时用两个变量,一个保存f[i-1],一个保存目前为止最大的f[i]复杂度:时间O(n),空原创 2014-05-14 17:51:16 · 1015 阅读 · 0 评论 -
Leetcode 动态规划 Decode Ways
题意:将A-B编码为1-26,现在给一串数字,问有多少种解码方式思路:动态规划设f[i]表示以第i个字符结尾的数字串的解码方式,则如果 s[i - 2]是1 或 s[i - 2]是2且s[i - 1]小于6,f[i] = f[i - 1] + f[i - 2] 否则,f[i] = f[i - 1]此外,还要再加一些判断如果s[i - 1]是零,则以s[i - 2]数字结尾的解码方式为零。因为s[i - 2]数字必须和s[i - 1]结合起来实现的时候只要两个变量保存前两个的值,即f[i - 1原创 2014-05-14 17:41:31 · 848 阅读 · 0 评论