- 博客(225)
- 收藏
- 关注
原创 LeetCode Word Ladder
题目Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence frombeginWord to endWord, such that: Only one letter can be changed at a timeEa
2015-05-27 09:31:05
572
原创 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
2015-05-15 10:47:11
457
原创 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.
2015-05-07 13:20:22
570
原创 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 dayi.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may
2015-05-06 13:13:15
493
原创 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 dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy
2015-05-04 13:12:47
452
原创 LeetCode 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 dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stoc
2015-05-04 13:06:20
460
原创 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],
2015-04-24 10:42:49
376
原创 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?与上一题相似,不过
2015-04-21 10:46:13
436
原创 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]]C(N,i)的展开,简单地按层递推,逐
2015-04-21 10:38:21
370
原创 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 constant
2015-04-16 12:58:58
391
原创 PAT A 1099. Build A Binary Search Tree (30)
题目A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys less than the node's key.The righ
2015-04-12 10:47:28
1034
原创 PAT A 1098. Insertion or Heap Sort (25)
题目According to Wikipedia:Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input d
2015-04-07 11:03:18
2540
原创 PAT A 1097. Deduplication on a Linked List (25)
题目Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value
2015-04-02 10:39:10
1008
原创 PAT A 1096. Consecutive Factors (20)
题目Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now gi
2015-04-01 13:47:52
1526
原创 PAT A 1095. Cars on Campus (30)
题目Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, y
2015-03-22 14:38:07
583
原创 PAT A 1094. The Largest Generation (25)
题目A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.In
2015-03-19 18:20:38
867
原创 PAT A 1093. Count PAT's (25)
题目The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.
2015-03-17 15:12:24
564
原创 PAT A 1092. To Buy or Not to Buy (20)
题目Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only
2015-03-17 14:48:24
949
原创 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.
2015-02-10 11:34:45
416
原创 LeetCode Distinct Subsequences
题目iven a string S and a string T, count the number of distinct subsequences ofT in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be
2015-02-09 12:20:33
364
原创 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:
2015-02-08 15:21:23
376
原创 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 / \
2015-02-07 13:59:13
370
原创 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 s
2015-01-26 10:22:08
338
原创 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. 求二叉树的最小深度,递归即可。 代码/** *
2015-01-20 13:23:09
369
原创 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 ofevery node never d
2015-01-19 10:51:57
513
原创 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. 将排序好的链转换为二叉查找树。问题和之前的相似,但是由于链不能随机访问,如果反复顺序读取相应位置代价太大,显然不可取。递归求解,先递归求解左子树,同时修改根
2015-01-15 13:21:18
467
原创 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. 将已经排序好的数组转换为平衡二叉查找树。 代码:/** * Definition for binary tree * struct TreeNode { * in
2015-01-14 13:02:22
470
原创 LeetCode Binary Tree Level Order Traversal II
题目Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7}
2015-01-12 12:55:16
440
原创 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. 根据中序遍历和后续遍历构建树。后续遍历某子树的最后一个元素是该子树的根,在中序遍历中寻找相应
2015-01-08 13:03:49
499
原创 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. 根据先序遍历和中序遍历构建树。先序遍历某子树的第一个元素是该子树的根,在中序遍历中寻找相应元素
2015-01-08 12:51:22
572
原创 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. 求最大深度,dfs 代码/** * Defini
2015-01-06 12:38:36
385
原创 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
2015-01-04 13:38:08
445
原创 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
2015-01-04 13:30:47
386
原创 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 th
2014-12-24 10:59:39
433
原创 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. 比较两棵
2014-12-24 10:48:07
383
原创 LeetCode Recover Binary Search Tree
题目Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure. 根据上一题的合法性判断,稍作修改,记录限制元素的位置,可以寻找到不合法的位置,和与之冲突的位置。 根据交换的元素所处的位置,
2014-12-22 10:47:30
402
原创 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 ke
2014-12-19 10:40:15
431
原创 LeetCode Interleaving String
题目Given s1, s2, s3, find whether s3 is formed by the interleaving ofs1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc",
2014-12-17 14:08:02
391
原创 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
2014-12-16 11:12:45
461
原创 LeetCode Unique Binary Search Trees
题目Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1
2014-12-15 10:29:24
402
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人