摘要: Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation ofs1="great": great / \ gr eat / \ / \g r e at / \ a tTo scramble the string, we may choose any non-leaf no... 阅读全文
posted @ 2012-11-19 22:16 chkkch 阅读(2191) 评论(0) 推荐(0) 编辑
摘要: Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/http://www.cnblogs.com/c/", =>"/c"Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return"/". 阅读全文
posted @ 2012-11-19 21:13 chkkch 阅读(1959) 评论(0) 推荐(0) 编辑
摘要: 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 constant space solution?这题用O(n)的辅助空间比较好做,中序遍历后排个序O(nlogn)。但要不改变树的结构来完成就比较难了。我只能想到一个把bst转为double link list后 阅读全文
posted @ 2012-11-19 19:50 chkkch 阅读(2550) 评论(1) 推荐(0) 编辑
摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2012-11-19 19:20 chkkch 阅读(4879) 评论(0) 推荐(1) 编辑
摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文
posted @ 2012-11-19 17:34 chkkch 阅读(1009) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values in the list, only nodes itself can be changed.利用三个指针p, pPre, pPrePr 阅读全文
posted @ 2012-11-19 17:07 chkkch 阅读(3258) 评论(0) 推荐(1) 编辑
摘要: You are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.For example, given:S:"barfoothefoobarman"L:["foo", " 阅读全文
posted @ 2012-11-19 16:57 chkkch 阅读(1467) 评论(1) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3.关键是记录三个指针,当前指针p, p的父亲pPre, pPre的父亲pPrePre。 1 /** 2 * 阅读全文
posted @ 2012-11-19 15:23 chkkch 阅读(1378) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3.首先我们把key设为非头节点的值,然后遍历链表,当节点值等于key时删除,不能于时更新key. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode 阅读全文
posted @ 2012-11-19 14:58 chkkch 阅读(1208) 评论(0) 推荐(0) 编辑
摘要: Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3- 阅读全文
posted @ 2012-11-19 14:45 chkkch 阅读(1499) 评论(0) 推荐(0) 编辑