微博上
@陳利人會(huì)不定時(shí)的發(fā)一些程序員的面試題,覺(jué)得挺有意思,有時(shí)會(huì)去寫(xiě)代碼做一下。最近他開(kāi)了個(gè)微信公眾號(hào):待字閨中 (id: daiziguizhongren),推送這些面試題目和網(wǎng)友的一些好的解答??吹剿龅拿嬖囶}目是促使我開(kāi)這個(gè)博客的另一個(gè)重要原因。:)
本文正是關(guān)于他出的題目:給一個(gè)整數(shù)數(shù)組,找到其中包含最多連續(xù)數(shù)的自己,比如給:15,7,12,6,14,13,9,11,則返回5:[11,12,13,14,15]。最簡(jiǎn)單的方法是sort然后scan一遍,但是要O(nlgn)。有什么O(n)的方法嗎?
跟他確認(rèn)了下,對(duì)空間復(fù)雜度有沒(méi)有什么要求,能不能用map這樣的輔助數(shù)據(jù)結(jié)構(gòu),他說(shuō)沒(méi)有限制。
我的想法就是,用一個(gè)map<int, int>,它的key是一個(gè)起始的數(shù)字,value是這個(gè)起始數(shù)字起連續(xù)的個(gè)數(shù)。這樣這個(gè)數(shù)組遍歷一遍下來(lái),只要map維護(hù)好了,自然就能得到最長(zhǎng)的連續(xù)子串了,并且算法復(fù)雜度應(yīng)該是O(n)。(不考慮map函數(shù)實(shí)現(xiàn)的復(fù)雜度)
前面說(shuō)了維護(hù)好map就可以了,那么怎么來(lái)維護(hù)這個(gè)map呢?
- 取出當(dāng)前的整數(shù),在map里看一下是否已經(jīng)存在,若存在則直接取下一個(gè),不存在轉(zhuǎn)2 (為什么要看是否已經(jīng)存在,因?yàn)轭}目沒(méi)有說(shuō)不會(huì)有重復(fù)的數(shù)字。)
- 查看下map里面當(dāng)前數(shù)字的前一個(gè)是否存在,如果存在,當(dāng)前的最長(zhǎng)長(zhǎng)度就是前一個(gè)最長(zhǎng)長(zhǎng)度+1
- 查看下map里面當(dāng)前數(shù)字的后一個(gè)是否存在,如果存在,那么就將以下一個(gè)數(shù)字開(kāi)始的子串的最后一個(gè)更新下,因?yàn)楸緛?lái)沒(méi)有連上的2個(gè)子串,因?yàn)楫?dāng)前數(shù)字的出現(xiàn)連起來(lái)了
- 接著再看下前面數(shù)字是否存在,如果存在,就更新以這個(gè)數(shù)字結(jié)尾的子串的第一個(gè)數(shù)字的連續(xù)子串長(zhǎng)度,原因同上
算法就是如上所示了,我們拿例子演練一遍
1) 首先給定15,這個(gè)時(shí)候map里面沒(méi)有15也沒(méi)有14和16,那么這個(gè)執(zhí)行完了之后map是map[15] = 1;
2) 然后遇到7,同上,也沒(méi)有6,7和8,所以執(zhí)行玩了之后變成map[7]=1, map[15]=1;
3) 12同上,map[7]=1, map[12]=1, map[15]=1;
4) 接下來(lái)是6,6就不一樣了,因?yàn)?存在的,所以執(zhí)行上面第3步之后,map[6]=2,map[7]=2,map[12]=1,map[15]=1;
5) 14的情況跟6一樣,結(jié)果是map[6]=2,map[7]=2,map[12]=1,map[14]=2,map[15]=2;
6) 13的情況相對(duì)復(fù)雜一些,因?yàn)?2和14都存在了 ,所以它會(huì)執(zhí)行以上1,2,3,4的所有4步:首先12存在,所以13的最長(zhǎng)子串是2,14存在,所以會(huì)更新到14起始的最后一個(gè)數(shù)字的最長(zhǎng)長(zhǎng)度,這里就是15的長(zhǎng)度=它自己的加上13的長(zhǎng)度,也就是4,同時(shí)我們把13的長(zhǎng)度也改成4,最后因?yàn)?2存在,我們要更新以12結(jié)尾的連續(xù)子串的開(kāi)始處,本例中就是12自己,12對(duì)應(yīng)更新成4
7) 最后是11,11的前面一個(gè)數(shù)字不存在,后一個(gè)數(shù)字存在,也就是要執(zhí)行以上1,3,第3步結(jié)束的時(shí)候已經(jīng)是11和15都更新成5了。最后的結(jié)果也就是5,并且是從11起始的。
下面上代碼:
1 int find_longest_consecutive_items(int *list, int size) {
2 map<
int,
int> mapping;
3 int max = 0;
4 // The start point for the longest chain
5 int start = 0;
6
7 for (
int i=0; i<size; i++) {
8 if (mapping.find(list[i]) == mapping.end()) {
9 int cur = list[i];
10 // Set current position as the start point for this potential longest chain
11 int cur_start = cur;
12 mapping.insert(make_pair(cur, 1));
13
14 map<
int,
int>::iterator prev = mapping.find(cur - 1);
15 map<
int,
int>::iterator next = mapping.find(cur + 1);
16
17 if (prev != mapping.end()) {
18 // If previous number exists, increase current consecutive count
19 mapping[cur] = prev->second + 1;
20 }
21
22 if (next != mapping.end()) {
23 // Update the last one in the chain with the consecutive count from the one before current position
24 int last = next->first + next->second - 1;
25 mapping[last] = mapping[cur] = mapping[cur] + mapping[last];
26 }
27
28 if (prev != mapping.end()) {
29 // Update the first one in the chain with the consecutive count from the one after current position
30 int first = prev->first - prev->second + 1;
31 mapping[first] = mapping[cur];
32
33 // Use the first one as the start point for the whole chain
34 cur_start = first;
35 }
36
37 if (mapping[cur_start] > max) {
38 start = cur_start;
39 max = mapping[cur_start];
40 }
41 }
42 }
43
44 cout << "Longest consecutive items:";
45 for (
int i=0; i<max; i++) {
46 cout << " " << start + i;
47 }
48 cout << endl;
49
50 return max;
51 }
完整代碼