
LeetCode解题报告
文章平均质量分 78
集中探讨LeetCode上经典面试题的解法,以Java和C++两种语言为主,在自己学习的同时希望能给他人带去一些帮助。
ljiabin
在IT的世界里不断成长!
展开
-
【LeetCode】Serialize and Deserialize Binary Tree 解题报告
【题目】Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection原创 2015-10-28 21:38:33 · 12701 阅读 · 0 评论 -
【LeetCode】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 Case原创 2015-09-10 21:09:13 · 2326 阅读 · 0 评论 -
【LeetCode】House Robber I & II 解题报告
【题目】IYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that a原创 2015-07-19 21:13:15 · 4504 阅读 · 0 评论 -
【LeetCode】Basic Calculator 解题报告
【题目】Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integer原创 2015-06-14 11:19:17 · 9072 阅读 · 0 评论 -
【LeetCode】Implement Stack using Queues 解题报告
【题目】Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- R原创 2015-06-14 10:35:53 · 9997 阅读 · 1 评论 -
【LeetCode】Course Schedule II 解题报告
【题目】There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is exp原创 2015-05-19 17:13:08 · 9463 阅读 · 0 评论 -
【LeetCode】Course Schedule 解题报告
【题目】There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is exp原创 2015-05-19 17:05:08 · 17658 阅读 · 0 评论 -
【LeetCode】Word Search 解题报告
【题目】Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or原创 2015-05-19 16:39:22 · 4667 阅读 · 0 评论 -
【LeetCode】Word Search II 解题报告
【题目】Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are原创 2015-05-19 16:52:33 · 16046 阅读 · 0 评论 -
【LeetCode】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原创 2014-12-12 11:21:52 · 7288 阅读 · 2 评论 -
【LeetCode】 Permutations 排列生成算法之字典序法
字典序排序生成算法字典序法就是按照字典排序的思想逐一产生所有排列。例如,由1,2,3,4组成的所有排列,从小到大的依次为:1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132,原创 2015-04-08 17:31:21 · 5717 阅读 · 1 评论 -
【LeetCode】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原创 2015-04-10 15:29:05 · 1974 阅读 · 1 评论 -
【LeetCode】Text Justification 解题报告
【题目】Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approa原创 2015-04-10 11:26:22 · 5745 阅读 · 1 评论 -
【LeetCode】Number of Islands 解题报告
【题目】Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Yo原创 2015-04-10 10:16:24 · 19761 阅读 · 1 评论 -
【LeetCode】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 t原创 2015-04-07 17:28:16 · 2803 阅读 · 0 评论 -
【LeetCode】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].【解析】题意:有很多个区间,把有重叠的区间合并。思路:先排序,然后检查相邻两原创 2015-04-06 20:32:50 · 5901 阅读 · 0 评论 -
【LeetCode】Longest Valid Parentheses 解题报告
【题目】Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring i原创 2015-04-06 19:05:15 · 2319 阅读 · 1 评论 -
【LeetCode】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:原创 2015-04-06 11:37:37 · 2832 阅读 · 0 评论 -
【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益。分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。**代码:**O(n)时原创 2015-04-06 11:10:34 · 24942 阅读 · 1 评论 -
【LeetCode】Binary Tree Right Side View 解题报告
【题目】Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tre原创 2015-04-06 10:33:34 · 9143 阅读 · 0 评论 -
【LeetCode】Search in Rotated Sorted Array II 解题报告
【题目】Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target原创 2015-04-02 15:00:36 · 1089 阅读 · 0 评论 -
【LeetCode】Scramble String 解题报告
【题目】Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great /原创 2015-03-22 15:16:18 · 5628 阅读 · 0 评论 -
【LeetCode】Interleaving String 解题报告
【题目】Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbb原创 2015-03-21 18:50:13 · 3985 阅读 · 1 评论 -
【LeetCode】Recover Binary Search Tree 解题报告
【题目】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原创 2015-03-21 11:58:37 · 3304 阅读 · 1 评论 -
【LeetCode】Repeated DNA Sequences 解题报告
【题目】All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the D原创 2015-03-20 11:19:21 · 6266 阅读 · 7 评论 -
【LeetCode】Binary Tree Maximum Path Sum 解题报告
【题目】Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Re原创 2015-01-22 16:15:05 · 4874 阅读 · 0 评论 -
【LeetCode】Largest Number 解题报告
【题目】Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result原创 2015-01-13 15:04:39 · 12707 阅读 · 13 评论 -
【LeetCode】Dungeon Game 解题报告【Solution】
【题目】The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was i原创 2015-01-11 21:41:08 · 9631 阅读 · 0 评论 -
【LeetCode】Longest Consecutive Sequence 解题报告
【题目】Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence i原创 2015-01-04 21:51:28 · 1266 阅读 · 0 评论 -
【LeetCode】Substring with Concatenation of All Words 解题报告
【题目】You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once an原创 2014-12-29 15:45:19 · 8926 阅读 · 6 评论 -
【LeetCode】First Missing Positive 解题报告
【题目】Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and use原创 2014-12-28 21:00:14 · 5256 阅读 · 0 评论 -
【LeetCode】Merge k Sorted Lists 解题报告
【题目】Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.合并几个有序链表为一个,分析算法复杂度。【分治】直观的想法是两两合并,有两种方法:1)list1和list2合并为newlist2,newlist2再和list3合原创 2014-12-26 10:43:07 · 1265 阅读 · 0 评论 -
【LeetCode】Multiply Strings 解题报告
【题目】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.【解析】题意:两个字符串表示的非负整数相乘,用字符串的原创 2014-12-23 10:47:36 · 2015 阅读 · 0 评论 -
【LeetCode】Restore IP Addresses 解题报告
【题目】Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"原创 2014-12-19 17:03:40 · 2291 阅读 · 0 评论 -
【LeetCode】Fraction to Recurring Decimal【Solution】
【题目】Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in paren原创 2014-12-19 11:13:19 · 13682 阅读 · 1 评论 -
【LeetCode】Distinct Subsequences 解题报告
【题目】Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (原创 2014-12-18 10:55:11 · 2559 阅读 · 0 评论 -
【LeetCode】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.【解析】题意:把链表循环右移k个结点。不是原创 2014-12-17 17:41:25 · 1320 阅读 · 0 评论 -
【LeetCode】Reverse Nodes in k-Group 解题报告
【题目】Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remai原创 2014-12-17 17:01:26 · 2473 阅读 · 0 评论 -
【LeetCode】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], an原创 2014-12-17 11:26:03 · 1905 阅读 · 0 评论 -
【LeetCode】Combination Sum I & II 解题报告
【Combination Sum I】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 cho原创 2014-12-16 11:06:12 · 7325 阅读 · 4 评论