随笔分类 -  算法

摘要:buildMaxHeap方法 buildMaxHeap方法的流程简单概括起来就是一句话,从A.length / 2一直到根结点进行maxHeapify调整。下面是图解。 Java代码 public static void maxHeapify(int[] a, int i, int length) 阅读全文
posted @ 2016-04-07 17:19 很好玩 阅读(22924) 评论(0) 推荐(4) 编辑
摘要:思路: 思路比较简单,将大数据的每位进行加法,记录进位即可。需要注意的是,两个数据中较长的一个需要另外单独处理多出的部分,在程序最后还要加上最高位的进位。 代码: 阅读全文
posted @ 2016-03-26 11:55 很好玩 阅读(459) 评论(0) 推荐(1) 编辑
摘要:题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 思路: 利用两个stack,一个表示单词,一个表 阅读全文
posted @ 2016-02-11 16:50 很好玩 阅读(297) 评论(0) 推荐(0) 编辑
摘要:题目: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to sho 阅读全文
posted @ 2016-02-11 10:22 很好玩 阅读(375) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For 阅读全文
posted @ 2016-02-06 23:17 很好玩 阅读(243) 评论(0) 推荐(0) 编辑
摘要:题目: 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 阅读全文
posted @ 2016-02-06 22:22 很好玩 阅读(403) 评论(0) 推荐(0) 编辑
摘要:题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路: 找到数组的中间数据作为根节点,小于中间数据的数组来构造作为左子树,大于中间数据的数组来构 阅读全文
posted @ 2016-02-05 13:47 很好玩 阅读(202) 评论(0) 推荐(0) 编辑
摘要:题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左 阅读全文
posted @ 2016-02-05 13:29 很好玩 阅读(288) 评论(0) 推荐(0) 编辑
摘要:题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就 阅读全文
posted @ 2016-02-05 10:48 很好玩 阅读(205) 评论(0) 推荐(0) 编辑
摘要:题目: 求完全二叉树节点数。 思路: 满二叉树的节点数是2^k-1,k是树的深度。 所以我们可以先判断该树是否为满二叉树,然后是的话直接返回结果,如果不是递归地求解子树。 这样不用遍历所有的节点。复杂度小于O(N),比对所有点遍历复杂度要小,最好的情况是O(lgN)。 推算大概在O(lgN)~O(N 阅读全文
posted @ 2016-02-05 09:58 很好玩 阅读(239) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 阅读全文
posted @ 2016-01-29 18:02 很好玩 阅读(400) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ B 阅读全文
posted @ 2016-01-29 14:58 很好玩 阅读(202) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The l 阅读全文
posted @ 2016-01-29 11:17 很好玩 阅读(375) 评论(0) 推荐(0) 编辑
摘要:题目: 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 阅读全文
posted @ 2016-01-29 10:52 很好玩 阅读(266) 评论(0) 推荐(0) 编辑
摘要:题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to it 阅读全文
posted @ 2016-01-28 20:06 很好玩 阅读(212) 评论(0) 推荐(0) 编辑
摘要:题目: Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, 阅读全文
posted @ 2016-01-28 17:43 很好玩 阅读(318) 评论(0) 推荐(0) 编辑
摘要:题目: 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  阅读全文
posted @ 2016-01-28 16:45 很好玩 阅读(229) 评论(0) 推荐(0) 编辑
摘要:题目: 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 阅读全文
posted @ 2016-01-27 23:39 很好玩 阅读(210) 评论(0) 推荐(0) 编辑
摘要:题目: 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 阅读全文
posted @ 2016-01-27 18:19 很好玩 阅读(200) 评论(0) 推荐(0) 编辑
摘要:题目:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.思路:第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。因为fast的速度是sl... 阅读全文
posted @ 2016-01-25 11:08 很好玩 阅读(153) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示