/*
* Sort a linked list in O(n log n) time using constant space
complexity.
*/
public ListNode sortList(ListNode head) {
if(head == null || head.next == null)
return head;
//get the length of the liklist
int count = 0;
ListNode node = head;
while(node!=null){
count++;
node = node.next;
}
//break up to two list
int middle = count / 2;
... 阅读全帖
sorted matrix = each row/column is sorted
remember k=O(n^2)
write code to solve in n*lg n
----------------------
O(n^2) is simple and stupid --- QuickSelect
sorted matrix = each row/column is sorted
e.g.,
1 6 6
2 7 7
2 8 9
k=7
remember k=O(n^2)
write code to solve in n*lg n
----------------------
O(n^2) is simple and stupid --- QuickSelect!!!!!!!!
原题在这里: http://www.geeksforgeeks.org/median-of-stream-of-integers-runni
Given that integers are read from a data stream. Find median of elements
read so for in efficient way. For simplicity assume there are no duplicates.
For example, let us consider the stream 5, 15, 1, 3 …
After reading 1st element of stream - 5 -> median - 5
After reading 2nd element of stream - 5, 15 -> median - 10
After reading 3rd element of stream - 5, 15, 1 -> median - 5
After reading 4th element of stream - 5, 15, 1, 3 -... 阅读全帖
回国一趟回来做题很难进入状态了
Merge k sorted linked lists and return it as one sorted list. Analyze and
describe its complexity.
有人面试碰到这题么?我洋洋洒洒写了很多代码,OJ说Time Limit Exceeded
class Solution {
public:
ListNode *mergeKLists(vector &lists) {
int n = lists.size();
if (n==0) return NULL;
ListNode *ret = lists[0];
for (int i=1; i
{
ret = merge2Lists(ret, lists[i]);
}
return ret;
}
private:
ListNode *merge2Lists(ListNod... 阅读全帖
interface是这个 Iterable mergeKSortedIterators(Iterators[] iters)
也就是给每个array的iterator,然后merge.
我开始想的是用merge k sorted list的办法,用一个heap保存所有的iterator,看哪个
iterator的头个element最小,push to result,然后Move to next element。
需要一个iterator的comparator来sort iterators, 是我的comparator写的不对?还
是heap的operation错了?
不知有人做过这题吗,希望给点建议。。。。
public static class iteratorComp implements Comparator>{
public int compare(Iterator a, Iterator b){
int a1 = 0;
in... 阅读全帖
If I have N numbers
and I want to find out the top X of them
and sort the top X from largest to smallest,
what kind of sorting algorithm would you recommend? thanks
Those of you using Gmail probably notices this for quite a while: it cannot
sort by sender and so on, which has been embarrassingly high-lighted in
their competition with Office for California state business.
As if it's not embarrassing enough, Google ridiculously went out to accuse
California state of cooking the requests to what MSFT could offer to make it
difficult for Google to compete.
"If you ask us to sort your emails, then you are asking too much. In fact
thou shall ask not what Google s
sorting sometimes is useful. If I have 1000 mails and I remembered one of
them
is from a user whose name start with 'p' something, I can just sort by user
to
find him out.
It's inconvenient when you want to see all the senders sorted at the same
time as search only gives result for one sender. A sorted view makes it
easier to clean up or re-organize your mail box. It's a limitation when
optimizing for search over other indexing. Funny Google chose to blame
customers instead of admitting it's their own problem.
【 以下文字转载自 Melody_In_The_Wind 俱乐部 】
发信人: Slytherin (小余|小则|小成), 信区: Melody_In_The_Wind
标 题: 潜伏 (10)Professor Gobeijinggo's Sorting Hat
发信站: BBS 未名空间站 (Mon Mar 28 21:32:19 2011, 美东)
第10章 PROFESSOR GOBEIJINGGO'S SORTING HAT
西北狼的讲话让气氛一下子活跃起来。正在这个时候,一个瘦高的人从台上走到了前台
,挥了挥WAND,一顶帽子突然出现在了台上。
众人立刻安静下来。这个人脸色严峻,一身黑袍,却正是北京教授,PROFESSOR OF THE
DENFENSE AGAINST THE DARK SEX。
"我念到名字的人都上台来,"北京不带感情地说道。
"这是怎么回事情?"余则成悄悄问到。瑟瑟和幽幽也是一脸的茫然。
"自从出现了LORD OF HH后,这里的规矩就严格了,要先进行性征审查的。"一个声音说
到。余则成回头一看,声音来自一个一头卷发的英俊小男生。
"我叫梅醒,"那个... 阅读全帖
Hi, How to Sort XML Records in Visual Basic?
LZ编程为菜鸟级别, 请求immediate帮助, codes越详细越好, 可以酬谢上百包子。
Here I want to sort this xml file using the element .
My sample xml:
Code:
how can you sort data in a table? i don't believe they're
stored in a sequential way. when you retrievfe data using
sql, you can always specify sorting order
【 以下文字转载自 Programming 讨论区,原文如下 】
发信人: gjbaby (北京知青), 信区: Programming
标 题: How to sort chinese data?
发信站: The unknown SPACE (Wed May 29 11:14:30 2002) WWW-POST
for example,in Customers table,I got a list of chinese customers(in Chinese),
how sould I sort them
1)based on Bi3 Hua4(is it possible?)
2)based on Pinyin after translation
Now I got another table "Orders",how can I joinCustomers with Orders based on
the customer name?
Hi! I am using Comparator to do column sorting for a JTable. It works
good with appletviewer. When I am using IE, it complains :
java.lang.IncompatibleClassChangeError
below is my code for Comparator interface:
I guess it is some thing wrong with Collection.sort()
//*****************************************************************
class ColComparator implements Comparator {
protected int m_sortCol;
protected boolean m_sortAsc;
public ColC
【 以下文字转载自 Programming 讨论区 】
【 原文由 ayanami 所发表 】
Supppose I have a HashMap wordmap with a String key, a integer value.
[Key=>value]
What is the easiest way to sort them based on value?
I looked at SortMap, but it only sorts based on key.
Thanks!
What is a Hashtable? Hashtable use hashCode to index data, so you can not
"sort" the value, the only way you can do is to create another sorted list of
values.