leetcode
淹不死的水
这个作者很懒,什么都没留下…
展开
-
leetcode~Binary Tree Level Order Traversal
public class TreePrinter { public List> printTree(TreeNode root) { List> result = new ArrayList>(); if(root==null) return null; //使用队列实现 Queue queue = new LinkedList(); //入队列 queue.offer原创 2017-02-16 20:48:21 · 258 阅读 · 0 评论 -
leetcode~Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1].public class PostorderTraversal {原创 2017-02-17 13:43:07 · 461 阅读 · 0 评论 -
leetcode~Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2].public class InorderTraversal {原创 2017-02-17 13:41:46 · 362 阅读 · 0 评论 -
leetcode~Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3].public class PreorderTraversal {原创 2017-02-17 13:39:34 · 559 阅读 · 0 评论 -
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,null,null,15,7],原创 2017-02-16 21:15:43 · 322 阅读 · 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 devise a const原创 2017-02-18 16:11:25 · 269 阅读 · 0 评论 -
leetcode88~Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addition原创 2017-02-28 10:04:31 · 219 阅读 · 0 评论 -
leetcode101~Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric:1/ \ 2 2 / \ / \ 3 4 4 3 But the fol原创 2017-02-18 18:01:55 · 192 阅读 · 0 评论 -
leetcode100~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.该题比较简单,不作解释,不懂的话,欢迎原创 2017-02-18 16:33:07 · 465 阅读 · 0 评论 -
leetcode110~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 never differ by原创 2017-02-18 20:55:58 · 187 阅读 · 0 评论 -
leetcode114~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 \ 2 \ 3原创 2017-02-18 22:37:15 · 162 阅读 · 0 评论 -
leetcode21~Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.//递归解法 public ListNode mergeTwoLists2(ListNode l1, List原创 2017-02-28 11:37:55 · 238 阅读 · 0 评论 -
leetcode23~Merge k Sorted Lists Add to List
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 把k个有序的链表合并成一个新的有序链表 1 使用分治思想,借鉴于归并排序。数组的数递归划分,一分二,二分四…直至最后一个数单独的成为一组,然后再进行归并划分采用的递归实现,归并可以使用上题的方法。时间原创 2017-02-28 16:19:43 · 956 阅读 · 0 评论 -
leetcode116~Populating Next Right Pointers in Each Node
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,原创 2017-02-20 10:32:28 · 316 阅读 · 0 评论 -
leetcode117~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 extra space.原创 2017-02-20 15:14:24 · 221 阅读 · 0 评论 -
leetcode147~Insertion Sort List
Sort a linked list using insertion sort.Sort a linked list using insertion sort. 插入排序:把待排序列的数据插入到已经排序好的序列。对数组插入排序时,在比较元素时是从最后的位置开始比较,但是在链表中,每次都需要一个pre指针,从头结点开始遍历,因此需要维护一个dummyNode作为头结点,使对其他结点的操作具有一原创 2017-03-01 10:20:47 · 266 阅读 · 0 评论 -
leetcode148~Sort List Add to List
Sort List Sort a linked list in O(n log n) time using constant space complexity.O(nlogn)的排序算法有归并、快排和堆排序。快排用于双向链表比较多,单向链表适合用归并排序。关键是找到中间节点,这里采用了快慢指针,当快指针遍历结束后,慢指针所在的位置就是中间位置。然后以慢指针的下一个节点作为新的头部,并对其进原创 2017-03-01 11:21:16 · 359 阅读 · 0 评论 -
leetcode105~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 static TreeNode buildTr原创 2017-02-21 10:19:41 · 316 阅读 · 0 评论 -
leetcode106~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.根据中序遍历和后序遍历创建一棵二叉树。 思路与上题类似。 关键在于,上下限的选取。下面两种方法,不同在于上下限选取不一样。原创 2017-02-21 10:55:00 · 229 阅读 · 0 评论 -
leetcode41~First Missing Positive Add to List
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 constant sp原创 2017-03-02 08:59:37 · 229 阅读 · 0 评论 -
leetcode75~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,原创 2017-03-02 10:05:31 · 277 阅读 · 0 评论 -
leetcode96~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原创 2017-02-22 13:57:52 · 230 阅读 · 0 评论 -
leetcode95~Unique Binary Search Trees II
Given an integer 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原创 2017-02-22 21:56:57 · 191 阅读 · 0 评论 -
leetcode98~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. The right原创 2017-02-23 08:21:35 · 251 阅读 · 0 评论 -
leetcode108~Convert Sorted Array to Binary Search Tree
/* * Given an array where elements are sorted in ascending order, convert it to a height balanced BST. * 注意递归的退出 * * 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary原创 2017-02-24 12:23:23 · 265 阅读 · 0 评论 -
leetcode111~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./* * 求二叉树的最小深度 * 最小深度:从根节点到叶节点走过的最短路径原创 2017-02-24 15:42:35 · 295 阅读 · 0 评论 -
leetcode109~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./* * Given a singly linked list where elements are sorted in ascending order, convert it t原创 2017-02-24 15:46:43 · 223 阅读 · 0 评论 -
leetcode524~Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return原创 2017-03-04 09:33:41 · 284 阅读 · 0 评论 -
leetcode104~Maximum Depth of Binary Tree Add to List
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.求解二叉树的深度,该题比较简单~~~ 两种解法,递归和非递归 非递归使用层序遍原创 2017-02-25 09:12:48 · 350 阅读 · 0 评论 -
leetcode350~Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times原创 2017-03-04 10:27:40 · 261 阅读 · 0 评论 -
leetcode349~Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can原创 2017-03-04 10:41:18 · 232 阅读 · 0 评论 -
leetcode27~Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The原创 2017-03-08 08:39:20 · 201 阅读 · 0 评论 -
leetcode31~Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible o原创 2017-03-08 10:12:38 · 253 阅读 · 0 评论 -
leetcode60~Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): “123”原创 2017-03-08 11:09:55 · 275 阅读 · 0 评论 -
leetcode113~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 / \原创 2017-02-25 12:11:48 · 220 阅读 · 0 评论 -
leetcode112~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原创 2017-02-25 10:16:27 · 283 阅读 · 0 评论 -
leetcode242~Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false. Note: You may assume原创 2017-03-04 18:39:03 · 256 阅读 · 0 评论 -
leetcode280~Wiggle Sort
摆动排序 给你一个没有排序的数组,请将原数组就地重新排列满足如下性质nums[0] <= nums[1] >= nums[2] <= nums[3]…. 请就地排序数组,也就是不需要额外数组 给出数组为 nums = [3, 5, 2, 1, 6, 4] 一种输出方案为 [1, 6, 2, 5, 3, 4]public class WiggleSort { //先将数组进行排序,然后原创 2017-03-04 19:28:38 · 245 阅读 · 0 评论 -
leetcode532~K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in原创 2017-03-08 15:20:58 · 1656 阅读 · 0 评论 -
leetcode485~Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s.The maximu原创 2017-03-08 15:40:48 · 213 阅读 · 0 评论