摘要: 今天折腾个东东,想要把表格的表头固定,很多网站都有这样的浏览体验,所以也给自己加个。在网上搜索好多,不清不楚,不知所云,还不如自己写一个。既然自己写就得整理一下思路。 其实只需要把表头位置放到浏览器的可视区域顶部就行,表格的其它部分不需要控制。所以很简单,代码如下: 1 function tableProperty(id){ 2 //var tbls = document.getElementsByTagName("table"); 3 //this.tbl = tbls && tbls.length && tbls[0]; 4 if(!id 阅读全文
posted @ 2013-12-09 21:46 月窟仙人 阅读(233) 评论(0) 推荐(1) 编辑
摘要: 题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.You may not alter the values in the nodes, only nodes itself may be changed.Only constant memory is 阅读全文
posted @ 2013-12-07 17:27 月窟仙人 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.最长回文字串,原型应该是ACM一道题目,不过这个已经简化很多了,只要把每一个位置作为回文的中心,往两边匹配找到回文长度,返回这些长度中最大的即可。有一个要注意的地方是奇数与偶数的情况。代码: 1 string longestPalindrom.. 阅读全文
posted @ 2013-11-30 20:28 月窟仙人 阅读(131) 评论(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 BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 ... 阅读全文
posted @ 2013-11-29 09:11 月窟仙人 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 题目:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.两个结点被交换过了,那自然而然的想到找出两个结点就行了,而中序遍历又正好是升序序列,因此在中序遍历过程中找到两个不符合升序的结点即可,这里一定要保存一下前驱结点,刚开始没想到,捣鼓了半天过了一半case,后来才发现,代码: 1 void recoverTree(TreeNode *root) { 2 TreeNode* node1; 3 ... 阅读全文
posted @ 2013-11-24 17:22 月窟仙人 阅读(99) 评论(0) 推荐(0) 编辑
摘要: 题目:Given inorder and postorder traversal of a tree, construct the binary tree.跟之前的通过先序遍历和中序遍历构建二叉树一样,有个区别是,这次的根结点每次都从后序列表中从后向前扫。代码: 1 TreeNode *buildTree(vector &inorder, vector &postorder) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the same Solution ins... 阅读全文
posted @ 2013-11-24 13:56 月窟仙人 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 题目:Given preorder and inorder traversal of a tree, construct the binary tree.通过先序遍历和中序遍历构造二叉树。大概思路是递归的构造,因为先序遍历总是先访问根结点,所以很容易从先序列表中得到根,位于该结点右侧的就是子树,再由这个根结点从中序列表中找到,位于该值左侧的就是左子树,右侧的即为又子树。然后分别用同样的方法构建左、右子树,直到构造完成。代码: 1 TreeNode *buildTree(vector &preorder, vector &inorder) { 2 // IMPORTANT... 阅读全文
posted @ 2013-11-24 13:54 月窟仙人 阅读(210) 评论(0) 推荐(0) 编辑
摘要: 题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.转换成平衡二叉树,刚看到题目直接想到了中序,想了会没解决,于是一口气看了三部钢铁侠,“小辣椒”完美!好吧看完电影查了一下平衡二叉树的定义:在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树把定义改成递归的,一棵树是平衡二叉树首先左右子树是平衡二叉树,并且左右子树高度差小于等于1。这样递归定义后就出来解决方案了。每次把链表二分,中间结点为根,左侧的作为 阅读全文
posted @ 2013-11-17 03:10 月窟仙人 阅读(174) 评论(0) 推荐(0) 编辑
摘要: 题目: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 ofeverynode never differ by more than 1.思路比较简单: 先看左右子树:平衡->看左右子树的高度差,不超过1则当前树平衡,否则失衡;不平衡->当前树不平衡;代码如下, 1 bool isBalanced 阅读全文
posted @ 2013-11-16 18:33 月窟仙人 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 题目描述: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.一看到这个题目的时候,想一层一层的数一下,仔细一想貌似不太靠谱,要么改结点结构,要么把结点在第几层存到另外的数据结构中。再一想其实很简单,就像一个递归定义:当前树的minimum depth等于:1.左右子树都不为空时,为子树中较小的minimum depth+1;2.有一 阅读全文
posted @ 2013-11-16 17:34 月窟仙人 阅读(120) 评论(0) 推荐(0) 编辑