上一页 1 2 3 4 5 6 7 ··· 11 下一页

2013年7月25日

摘要: Problem:Implementint sqrt(int x).Compute and return the square root ofx.Analysis:If we simply use the naive algorithm to go through all the numbers from 0 until the square root, it will exceed the time limit. The hero is binary search.We want such a number between 0 and n inclusively that x*x n. No. 阅读全文
posted @ 2013-07-25 05:43 freeneng 阅读(236) 评论(0) 推荐(0) 编辑

2013年7月24日

摘要: Problem:Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].Analysis:First sort the vector according to the start time of each interval.Then scan through the vector, if the current interval's start time is betwee 阅读全文
posted @ 2013-07-24 13:13 freeneng 阅读(154) 评论(0) 推荐(0) 编辑
摘要: Problem:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.Analysis:There are two ways: one is the naive comparing algorithm and the other is KMP algorithm.For KMP algorithm, please refer the wiki pageFor the naive algorithm, w 阅读全文
posted @ 2013-07-24 05:17 freeneng 阅读(172) 评论(0) 推荐(0) 编辑
摘要: Problem:Given a binary tree struct 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, the next pointer should be set toNULL.Initially, all next pointers are set ... 阅读全文
posted @ 2013-07-24 01:31 freeneng 阅读(153) 评论(0) 推荐(0) 编辑

2013年7月23日

摘要: Problem: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 \ 4 \ 5 \ 6Analysis:Solution to t... 阅读全文
posted @ 2013-07-23 14:19 freeneng 阅读(178) 评论(0) 推荐(0) 编辑
摘要: Problem:Given a binary tree, return thebottom-up level ordertraversal 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}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [... 阅读全文
posted @ 2013-07-23 02:36 freeneng 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Problem:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Analysis:This problem is the same as the former one of reconstructing the binary tree with inorder and preorder traversal array.The only difference is that 阅读全文
posted @ 2013-07-23 02:21 freeneng 阅读(115) 评论(0) 推荐(0) 编辑
摘要: Problem:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Analysis:We know that, for a binary tree, the structure of inorder traversal is as follows:|...left subtree inorder...| root |...right subtree inorder...|and 阅读全文
posted @ 2013-07-23 02:06 freeneng 阅读(169) 评论(0) 推荐(0) 编辑

2013年7月22日

摘要: Problem:Given a binary tree, return thelevel ordertraversal 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 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]Analysis:The problem is an extensi... 阅读全文
posted @ 2013-07-22 15:13 freeneng 阅读(172) 评论(0) 推荐(0) 编辑
摘要: Problem: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 keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the lef 阅读全文
posted @ 2013-07-22 13:54 freeneng 阅读(233) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 ··· 11 下一页

导航