摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity.数据元素无序但要求时间复杂度O(n)应联想到hash.stl中unordere 阅读全文
posted @ 2014-03-16 21:42 小菜刷题史 阅读(179) 评论(0) 推荐(0) 编辑
摘要: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).更一般的问题是查找两个数据的第K小/大元素。除了利用归并过程查找外还有更优的方法:利用二分的思想,充分利用数组已经分别有序的条件,使得每比较一个数,就可以确定该数组中一部分元素是位于第K个位置之前还是之后。因为有两个数组,两者的元素之间大小关系未知,所以应该比较数组的第[k 阅读全文
posted @ 2014-03-16 20:35 小菜刷题史 阅读(133) 评论(0) 推荐(0) 编辑
摘要: 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. 1 class Solution { 2 public: 3 bool search(int A[], int n, int target) { 4 int start = 0, en.. 阅读全文
posted @ 2014-03-16 19:20 小菜刷题史 阅读(237) 评论(0) 推荐(0) 编辑
摘要: 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. 1 class Solution { 2 public: 3 阅读全文
posted @ 2014-03-16 18:58 小菜刷题史 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should ret... 阅读全文
posted @ 2014-03-16 16:50 小菜刷题史 阅读(97) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for a... 阅读全文
posted @ 2014-03-16 16:29 小菜刷题史 阅读(150) 评论(0) 推荐(0) 编辑