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...
分类:
其他好文 时间:
2014-12-03 22:50:41
阅读次数:
137
题目
Sort a linked list in O(n log n)
time using constant space complexity.
解答
O(nlogn)时间复杂度的排序有快排、堆排、归并,一般双向链表用快排、单向链表用归并,堆排两种都可以,以下使用归并排序:
/**
* Definition for singly-linked list.
* class Li...
分类:
其他好文 时间:
2014-12-03 21:32:25
阅读次数:
138
一、原题
Sort List
Sort a linked list in O(n log n)
time using constant space complexity.
二、分析
快速排序和归并排序的时间复杂度是O(nlogn)。如果使用归并排序,在使用链表的情况下,不需要重新申请空间存放排序后的数组,可以做到空间复杂度数O(1)。我在这里使用归并排序来排序链表...
分类:
其他好文 时间:
2014-12-03 21:28:07
阅读次数:
112
Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity...
分类:
其他好文 时间:
2014-12-03 14:01:20
阅读次数:
181
Sort a linked list inO(nlogn) time using constant space complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * L...
分类:
其他好文 时间:
2014-12-03 12:12:42
阅读次数:
188
题目描述: 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 sho....
分类:
其他好文 时间:
2014-12-02 18:57:38
阅读次数:
213
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 ...
分类:
其他好文 时间:
2014-11-30 19:56:07
阅读次数:
103
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BAN...
分类:
编程语言 时间:
2014-11-30 10:22:33
阅读次数:
241
Sort ListSort a linked list in O(n log n) time using constant space complexity.使用Merge Sort, 空间复杂度是 O(logN) 因为使用了栈空间。SOLUTION 1:使用Merge Sort来解决问题。为什么不...
分类:
其他好文 时间:
2014-11-29 21:28:58
阅读次数:
203
Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity...
分类:
其他好文 时间:
2014-11-29 14:30:53
阅读次数:
108