算法题
文章平均质量分 60
和道一文字JC
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
[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原创 2013-08-02 21:08:07 · 580 阅读 · 0 评论 -
[leetcode] 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 ret原创 2013-08-03 13:01:01 · 535 阅读 · 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 not found原创 2013-08-03 21:17:32 · 625 阅读 · 0 评论 -
[leetcode] Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.原创 2013-08-19 14:58:45 · 555 阅读 · 0 评论 -
[leetcode] Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1原创 2013-08-18 21:42:42 · 682 阅读 · 0 评论 -
[leetcode] Interleaving String@DP
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 = "aadbbbaccc",原创 2013-08-19 10:00:43 · 793 阅读 · 0 评论 -
[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 you devis原创 2013-08-19 15:49:30 · 597 阅读 · 0 评论 -
[leetcode] Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.class Solution {public: TreeNode *buildTree(vecto原创 2013-08-19 17:11:27 · 672 阅读 · 0 评论 -
[leetcode] 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 f原创 2013-08-19 17:03:43 · 692 阅读 · 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(vector &num) { // Start typing your C原创 2013-08-19 19:24:54 · 647 阅读 · 0 评论 -
[leetcode] Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.class Solution {public: TreeNode *sortedListToBST(ListNode *head) { // Sta原创 2013-08-19 19:25:39 · 775 阅读 · 0 评论 -
[leetcode] Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.class Solution {public: int m原创 2013-08-19 19:39:36 · 595 阅读 · 0 评论 -
[leetcod] 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 ofevery node never diff原创 2013-08-19 19:28:51 · 811 阅读 · 0 评论 -
[leetcode] Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.class Solu原创 2013-08-19 16:08:30 · 600 阅读 · 0 评论 -
[leetcode] Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.class Solution {public: TreeNode *buildTree(vector原创 2013-08-19 17:10:19 · 523 阅读 · 0 评论 -
[leetcode] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binar原创 2013-08-19 17:09:41 · 586 阅读 · 0 评论 -
[leetcode] 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原创 2013-08-19 20:27:52 · 654 阅读 · 0 评论 -
[leetcode] 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原创 2013-08-07 16:49:30 · 844 阅读 · 0 评论 -
[leetcode] 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 c原创 2013-08-07 22:34:28 · 631 阅读 · 0 评论 -
[leetcode] 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原创 2013-08-19 17:06:47 · 628 阅读 · 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 Solution {public: int m原创 2013-08-19 19:23:04 · 648 阅读 · 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 and sum原创 2013-08-19 19:55:57 · 626 阅读 · 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,Given a binary tree and a s原创 2013-08-19 20:11:26 · 653 阅读 · 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 3Return 6.cl原创 2013-08-20 22:07:51 · 659 阅读 · 0 评论 -
[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 uses consta原创 2013-08-07 22:57:54 · 512 阅读 · 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,2,1,2,原创 2013-08-08 10:15:00 · 545 阅读 · 0 评论 -
[leetcode] Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [原创 2013-08-20 21:26:09 · 635 阅读 · 0 评论 -
[leetcode] 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?class So原创 2013-08-20 22:02:03 · 579 阅读 · 0 评论 -
[leetcode] Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You ma原创 2013-08-20 22:05:51 · 851 阅读 · 0 评论 -
[leetcode] 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.原创 2013-08-20 21:46:34 · 668 阅读 · 0 评论 -
[leetcode] 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 constan原创 2013-08-20 21:48:18 · 618 阅读 · 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]]class Solution {public:原创 2013-08-20 21:54:03 · 590 阅读 · 0 评论 -
[leetcode] Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy原创 2013-08-20 22:04:33 · 1048 阅读 · 0 评论 -
[leetcode] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is no原创 2013-08-20 22:14:59 · 623 阅读 · 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 (can be n原创 2013-08-20 23:03:28 · 728 阅读 · 0 评论 -
[leetcode] Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediat原创 2013-08-21 10:19:04 · 625 阅读 · 0 评论 -
[leetcode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word原创 2013-08-21 19:45:01 · 947 阅读 · 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 is [1,原创 2013-08-21 20:40:37 · 777 阅读 · 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.大整数相乘需要一点技巧,如果用string的话会非常方便!!!usin原创 2013-08-08 16:10:12 · 567 阅读 · 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 the to原创 2013-08-21 21:18:05 · 729 阅读 · 0 评论
分享