- 博客(48)
- 资源 (1)
- 收藏
- 关注
原创 CODE 50: 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.Given 1->1-
2013-09-29 22:44:54 547
原创 CODE 51: 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. public ListNode dele
2013-09-29 22:04:44 448
原创 CODE 48: 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. public int maximalRectangle(char[][] matrix) { // Start typing your Java sol
2013-09-28 22:06:31 389
原创 CODE 49:Largest Rectangle in Histogram
public int largestRectangleArea(int[] height) { // Start typing your Java solution below // DO NOT write main() function if (null == height || height.length <= 0) { return 0; } int max =
2013-09-27 23:30:26 495
原创 CODE 47: 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 in each of
2013-09-25 22:30:06 697
原创 CODE 45: Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A
2013-09-23 22:43:47 411
原创 CODE 44: Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of
2013-09-22 22:52:19 523
原创 CODE 43: Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu
2013-09-22 21:55:13 506
原创 CODE 40: 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"]. (Order
2013-09-21 21:09:27 536
原创 CODE 39: Submission Details
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solutio
2013-09-21 20:00:05 542
原创 CODE 46: 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 / \ gr
2013-09-21 19:51:04 697
原创 CODE 37: 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 3
2013-09-21 11:01:06 623
原创 CODE 38: 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 \
2013-09-21 10:40:34 538
原创 CODE 36: 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 = "aadbbbaccc", ret
2013-09-20 23:36:14 593
原创 CODE 35: 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.Th
2013-09-20 21:39:24 636
原创 CODE 26: 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},
2013-09-20 19:38:56 683
原创 CODE 34: 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 devise a
2013-09-20 19:33:34 827
原创 CODE 33: 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. public
2013-09-20 18:35:57 731
原创 CODE 32: 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-09-20 18:30:19 681
原创 CODE 31: 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-09-20 18:08:53 674
原创 CODE 30: 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 binary tr
2013-09-20 18:06:20 741
原创 CODE 29: 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. public int maxDepth(TreeNode ro
2013-09-20 15:48:40 944
原创 CODE 28: 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. public TreeNode buildTree(int[] preorder, int[] inor
2013-09-20 15:43:46 935
原创 CODE 27: 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. public TreeNode buildTree(int[] inorder, int[] post
2013-09-20 13:31:37 956
原创 CODE 24: 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. public TreeNode sortedListToBST(ListNode head) { // Start typing your Java solution
2013-09-20 09:58:18 696
原创 CODE 25: Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. public TreeNode sortedArrayToBST(int[] num) { // Start typing your Java solution below // DO N
2013-09-20 09:18:09 648
原创 CODE 23: 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 differ
2013-09-19 21:07:00 662
原创 CODE 22: 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. public int minDepth(TreeNode root) {
2013-09-19 20:16:48 693
原创 CODE 20: Path Sum II
public ArrayList> pathSum(TreeNode root, int sum) { // Start typing your Java solution below // DO NOT write main() function ArrayList> paths = new ArrayList>(); if (null == root) { re
2013-09-19 19:11:32 613
原创 CODE 21: 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-09-18 23:16:25 574
原创 CODE 19: 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-09-17 22:31:52 545
原创 CODE 18: 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 none)
2013-09-17 21:16:00 505
原创 CODE 16: 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 extr
2013-09-16 21:23:51 703
原创 CODE 17: 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. If t
2013-09-16 21:22:44 604
原创 CODE 15: 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]]My Code: public
2013-09-16 20:59:27 634
原创 CODE 9: 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.
2013-09-16 20:53:06 782
原创 CODE 2: Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","b"],
2013-09-16 20:25:58 689
原创 CODE 14: 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? public Ar
2013-09-15 21:19:49 560
原创 CODE 13: 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-09-15 18:57:30 518
原创 CODE 10: 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 may no
2013-09-15 16:57:05 814
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人