leetcode
_海阔天空
这个作者很懒,什么都没留下…
展开
-
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 · 457 阅读 · 0 评论 -
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 · 554 阅读 · 0 评论 -
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 · 699 阅读 · 0 评论 -
CODE 3: Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region .For example,X X X XX O O XX X O原创 2013-09-09 21:30:24 · 694 阅读 · 1 评论 -
CODE 4: 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 number123.Find the total sum原创 2013-09-09 22:47:01 · 675 阅读 · 0 评论 -
CODE 5: 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, 2, 3,原创 2013-09-10 22:28:57 · 538 阅读 · 0 评论 -
CODE 8: 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 not a pa原创 2013-09-12 22:13:27 · 670 阅读 · 0 评论 -
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 · 790 阅读 · 0 评论 -
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 · 821 阅读 · 0 评论 -
CODE 11: 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 on原创 2013-09-15 16:56:50 · 851 阅读 · 0 评论 -
CODE 12: 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 day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2013-09-15 16:55:55 · 773 阅读 · 0 评论 -
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 · 527 阅读 · 0 评论 -
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 · 568 阅读 · 0 评论 -
CODE 1: Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return 1 s原创 2013-09-08 15:08:02 · 546 阅读 · 1 评论 -
CODE 7: Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence fromstart to end, such that:Only one letter can be changed at a time Each intermediate word原创 2013-09-12 00:05:40 · 667 阅读 · 0 评论 -
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 · 645 阅读 · 0 评论 -
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 · 710 阅读 · 0 评论 -
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 · 611 阅读 · 0 评论 -
CODE 54: 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 vertically n原创 2013-10-01 22:42:51 · 422 阅读 · 0 评论 -
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 · 554 阅读 · 0 评论 -
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 · 512 阅读 · 0 评论 -
CODE 55: Subsets
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example原创 2013-10-01 23:25:33 · 544 阅读 · 0 评论 -
CODE 56: Combinations
Given two integers n and k, return all possible combinations ofk numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2013-10-03 21:07:26 · 462 阅读 · 0 评论 -
CODE 57: Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".原创 2013-10-04 11:01:07 · 513 阅读 · 0 评论 -
CODE 58: 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 integers 0,原创 2013-10-04 20:52:26 · 625 阅读 · 0 评论 -
CODE 60: Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(mn) s原创 2013-10-05 10:07:35 · 493 阅读 · 0 评论 -
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 · 582 阅读 · 0 评论 -
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 · 705 阅读 · 0 评论 -
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 · 670 阅读 · 0 评论 -
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 · 620 阅读 · 0 评论 -
CODE 59: Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2013-10-04 22:22:18 · 526 阅读 · 0 评论 -
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 · 656 阅读 · 0 评论 -
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 · 703 阅读 · 0 评论 -
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 · 963 阅读 · 0 评论 -
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 · 946 阅读 · 0 评论 -
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 · 746 阅读 · 0 评论 -
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 · 688 阅读 · 0 评论 -
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 · 952 阅读 · 0 评论 -
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 · 683 阅读 · 0 评论 -
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 · 741 阅读 · 0 评论