
LeetCode
文章平均质量分 57
seemuch
这个作者很懒,什么都没留下…
展开
-
[CTCI] Hanoi
问题:In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size fr原创 2014-01-19 02:06:47 · 651 阅读 · 0 评论 -
[LeetCode] 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.分析:简单的一道递归题。代码:class原创 2014-01-05 08:46:04 · 395 阅读 · 0 评论 -
[LeetCode] 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 t原创 2014-01-05 08:42:31 · 453 阅读 · 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.分析:没有技巧,直接做。代码:class Solut原创 2014-01-05 07:36:26 · 416 阅读 · 0 评论 -
[LeetCode] Jump Game
问题:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.De原创 2014-01-04 23:21:35 · 456 阅读 · 0 评论 -
[LeetCode] Convert Sorted Array to Binary Search Tree
问题:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.分析:简单的一道递归题。代码:class Solution {public: TreeNode *sortedArrayToBST(const vector &num, i原创 2014-01-04 11:40:20 · 381 阅读 · 0 评论 -
[LeetCode] Path Sum
问题:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree原创 2014-01-04 11:20:07 · 363 阅读 · 0 评论 -
[LeetCode] Maximal Rectangle
问题:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.分析:参考Largest Rectangle in Histogram这道题以及这篇文章。我们面对的这道题可以转化为柱形图,然后用前面这道题原创 2014-01-04 07:52:44 · 395 阅读 · 0 评论 -
[LeetCode] Largest Rectangle in Histogram
问题:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram wher原创 2014-01-04 06:14:00 · 499 阅读 · 0 评论 -
[LeetCode] Median of Two Sorted Arrays
问题:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).分析:比较难的一道题。这里给出一个更加gener原创 2014-01-04 05:40:45 · 367 阅读 · 0 评论 -
[LeetCode] 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:"((()))", "(()())", "(())()", "()(()原创 2013-12-30 08:55:15 · 443 阅读 · 0 评论 -
[LeetCode] Add Two Numbers
问题:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2014-01-04 23:42:58 · 434 阅读 · 0 评论 -
[LeetCode] Jump Game II
问题:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Yo原创 2014-01-04 23:28:09 · 657 阅读 · 0 评论 -
[LeetCode] Pascal's Triangle
问题:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]分析:没有trick,直接做。原创 2014-01-04 11:28:49 · 399 阅读 · 0 评论 -
[LeetCode] Path Sum II
问题:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5原创 2014-01-04 11:21:22 · 407 阅读 · 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].分析:不难的一道题,但需要注意C++中的comparator的用法。参见这里。原创 2014-01-04 09:28:03 · 415 阅读 · 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.分析:注意,当找到split point之后,两原创 2014-01-04 04:34:10 · 367 阅读 · 0 评论 -
[LeetCode] Longest Common Prefix
问题:Write a function to find the longest common prefix string amongst an array of strings.分析:这道题的input是一个vector of strings。首先把这些string中的第一个拿出来作为标准。第一个是谁其实并不重要,它是不是最长的或最短的也不重要,因为common prefix不可能原创 2014-01-04 12:57:36 · 615 阅读 · 0 评论 -
[LeetCode] 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.Gi原创 2014-01-06 05:33:08 · 463 阅读 · 0 评论 -
[LeetCode] 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.分析:没什么可说的。代码原创 2014-01-06 05:37:39 · 398 阅读 · 0 评论 -
[总结] Binary tree traversal
一个binary tree有三种traversal的方式:preorder / inorder / postorder。我们分别来说:1. Preorder所谓preorder,就是处理任何children之前,先处理parent。比如leetcode上,Binary Tree Preorder Traversal 这道题中提供的例子:For example:Given原创 2014-01-17 06:42:43 · 858 阅读 · 0 评论 -
[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 3Ret原创 2014-01-17 08:18:16 · 512 阅读 · 0 评论 -
[LeetCode] Single Number
问题:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without原创 2014-01-16 13:54:27 · 447 阅读 · 0 评论 -
[LeetCode] Sort List
问题:Sort a linked list in O(n log n) time using constant space complexity.分析:用divide and conquer。首先遍历一次,找到长度;然后遍历一半,找到中间点;然后在左右两半recursion,然后merge。Time complexity: T(n) = 2T(n/2) + n = O(n原创 2014-01-11 04:18:14 · 437 阅读 · 0 评论 -
[LeetCode] Maximum Subarray
问题:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−原创 2014-01-10 13:47:37 · 431 阅读 · 0 评论 -
[LeetCode] Insertion Sort List
问题:Sort a linked list using insertion sort.分析:一般在array上实现的insertion sort,都是向之前的方向移动,找到自己的位置。而现在一个list不能向前移动,所以我们通过从头开始向后走的方式找到每一个node应该在的位置。List的题很考验功底,因为一个不小心哪里细节没注意就会出错。代码:class Solution原创 2014-01-10 10:49:16 · 452 阅读 · 0 评论 -
[LeetCode] Clone Graph
问题:Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separat原创 2014-01-10 06:38:50 · 487 阅读 · 0 评论 -
[LeetCode] 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-01-08 00:59:20 · 482 阅读 · 0 评论 -
[LeetCode] 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 rea原创 2014-01-07 08:22:33 · 382 阅读 · 0 评论 -
[LeetCode] 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原创 2014-01-07 02:01:38 · 468 阅读 · 0 评论 -
[LeetCode] Valid Sudoku
问题:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partia原创 2014-01-07 00:58:29 · 471 阅读 · 0 评论 -
[LeetCode] Trapping Rain Water
问题:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3原创 2014-01-06 23:47:13 · 950 阅读 · 0 评论 -
[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 (c原创 2014-01-07 09:22:10 · 482 阅读 · 0 评论 -
[LeetCode] 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原创 2014-01-06 23:32:43 · 343 阅读 · 0 评论 -
[LeetCode] Search for a Range
问题:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is n原创 2014-01-06 06:16:07 · 589 阅读 · 0 评论 -
[LeetCode] Balanced Binary Tree
问题:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node ne原创 2014-01-06 04:19:22 · 441 阅读 · 0 评论 -
[LeetCode] 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 th原创 2014-01-06 04:17:42 · 520 阅读 · 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 = "aadbbba原创 2014-01-04 02:24:22 · 465 阅读 · 0 评论 -
[LeetCode] Sum Root to Leaf Numbers
问题:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find原创 2014-01-03 08:28:50 · 390 阅读 · 0 评论 -
[LeetCode] 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.分析:试想一下,所有跟跟原点的斜率相同的点都在同一条直线上。同理,所有跟某一个固定点斜率相同的点都在同一条直线上。那么我们就用double for loop,其中外层的loop原创 2014-01-03 10:55:32 · 681 阅读 · 0 评论