摘要: Q:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.A:有重复的元素。bool bisearch(int A[],int begin,int end,int target){ if(begin>end) return false; 阅读全文
posted @ 2013-06-18 10:05 summer_zhou 阅读(164) 评论(0) 推荐(0) 编辑
摘要: Q:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.A: 递归+二分查找。 int bisearch(in... 阅读全文
posted @ 2013-06-17 21:33 summer_zhou 阅读(120) 评论(0) 推荐(0) 编辑
摘要: Q:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 ... 阅读全文
posted @ 2013-06-17 21:08 summer_zhou 阅读(202) 评论(0) 推荐(0) 编辑
摘要: Q: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 toNULL... 阅读全文
posted @ 2013-06-17 16:04 summer_zhou 阅读(189) 评论(0) 推荐(0) 编辑
摘要: Q:Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note:A word is defined as a character sequence consists of non-space characters only.For example,Givens="Hello Wo 阅读全文
posted @ 2013-06-16 17:37 summer_zhou 阅读(151) 评论(0) 推荐(0) 编辑
摘要: Q:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.For example,Givenboard=[ [ 阅读全文
posted @ 2013-06-16 17:01 summer_zhou 阅读(175) 评论(0) 推荐(0) 编辑
摘要: Q: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.A: 要注意k=0,k>=n等这些特殊情况。为了防止k>>n的情况,先计算链表的长度n,然后对k取余数 ListNode *rotateRight(ListNode *head, int k) { // Start typing 阅读全文
posted @ 2013-06-16 14:40 summer_zhou 阅读(114) 评论(0) 推荐(0) 编辑
摘要: Q: 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. ListNode *swapPai 阅读全文
posted @ 2013-06-15 21:06 summer_zhou 阅读(141) 评论(0) 推荐(0) 编辑
摘要: //DFS超时了。 void dfs(vector<vector<int> > &triangle, int depth, int offset,int curSum,int &min) { if(depth==triangle.size()) { if(min>curSum) min = curSum; return; } curSum += triangle[depth][offset]; dfs(triangle,depth+1,off... 阅读全文
posted @ 2013-06-14 21:51 summer_zhou 阅读(116) 评论(0) 推荐(0) 编辑
摘要: Q:Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Givennwill always be valid.Try to do this in one pass. 阅读全文
posted @ 2013-06-14 20:26 summer_zhou 阅读(208) 评论(0) 推荐(0) 编辑