上一页 1 2 3 4 5 6 7 8 9 ··· 17 下一页

2013年11月23日

Scramble String

摘要: 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 @ 2013-11-23 10:51 waruzhi 阅读(216) 评论(0) 推荐(0) 编辑

2013年11月22日

Insert Interval

摘要: Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5],[6,7],[8 阅读全文

posted @ 2013-11-22 21:41 waruzhi 阅读(173) 评论(0) 推荐(0) 编辑

Implement strStr()

摘要: Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.思路:KMP算法。这道题理解KMP花了很久的时间,在求next函数的时候使用递推的思路,假如a[i] = k, 如果a[i+1] = a[k],那么a[i+2] != a[k+1]时,a[i+1] = k+1, 否则k = next[k],相当于在已匹配的子串部分再使用KMP。理解这一部分就基本理解全部了。代码: 1 void getNext... 阅读全文

posted @ 2013-11-22 19:41 waruzhi 阅读(164) 评论(0) 推荐(0) 编辑

2013年11月14日

Permutation Sequence

摘要: The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not 阅读全文

posted @ 2013-11-14 20:02 waruzhi 阅读(134) 评论(0) 推荐(0) 编辑

Insertion Sort List

摘要: Sort a linked list using insertion sort.代码: 1 ListNode *insertionSortList(ListNode *head) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the same Solution instance will be reused for each test case. 4 if(!head) 5 return NULL; 6 Li... 阅读全文

posted @ 2013-11-14 11:12 waruzhi 阅读(162) 评论(0) 推荐(0) 编辑

2013年11月13日

Restore IP Addresses

摘要: Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)思路:DFS。只需增加判断每个部分数字在0到255之间,并且第一位数不能是0.另外代码中用到string转int的方法,先利用c_s 阅读全文

posted @ 2013-11-13 18:25 waruzhi 阅读(185) 评论(0) 推荐(0) 编辑

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 constant space solution?思路:如果不限定空间的话,直接的办法是中序遍历的值放入一个数组,然后遍历这个数组,找到第一个前面大于后面的数的较大的数,再找最后一个前面大于后面的数的较小的数。因为 阅读全文

posted @ 2013-11-13 15:46 waruzhi 阅读(181) 评论(0) 推荐(0) 编辑

Rotate List

摘要: Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.代码: 1 ListNode *rotateRight(ListNode *head, int k) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the s 阅读全文

posted @ 2013-11-13 15:12 waruzhi 阅读(167) 评论(0) 推荐(0) 编辑

First Missing Positive

摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思路:要求用O(n)的时间想到用hash,又要求用constant space就考虑使用原数组的空间来实现hash。其中一种思路是把A[i]映射到A[A[i]-1]上,把A[A[i]-1]赋值为负表示A[i]已被映射。最后从 阅读全文

posted @ 2013-11-13 14:57 waruzhi 阅读(279) 评论(0) 推荐(0) 编辑

Longest Palindromic Substring

摘要: Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longest palindromic substring.思路一:中心扩展法,复杂度O(n^2)思路二:动规,复杂度O(n^2)思路三:manacher算法,复杂度O(n)。以下为参考资料:举例说明:对字符串S=abcdcba而言,最长回文子串是以d为中心,半径为3的子串。当我们采用上面的做法分别求出以S[1]=a, 阅读全文

posted @ 2013-11-13 10:09 waruzhi 阅读(341) 评论(0) 推荐(0) 编辑

上一页 1 2 3 4 5 6 7 8 9 ··· 17 下一页

导航