青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

S.l.e!ep.¢%

像打了激速一樣,以四倍的速度運轉(zhuǎn),開心的工作
簡單、開放、平等的公司文化;尊重個性、自由與個人價值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

題目描述:

Given an array of n integers, such that each number in the array appears exactly twice, except for three numbers (say a, b and c) which appear exactly once.

In O(n) time and O(1) space find a,b and c.

分析:

先看這樣一道題:

n個數(shù)中有且僅有一個數(shù)出現(xiàn)了奇數(shù)次(其它數(shù)都出現(xiàn)了偶數(shù)次),如何用線性時間常數(shù)空間找出這個數(shù)。

解法如下:

從頭到尾異或一遍,結(jié)果就是要求的那個數(shù)。(原理很清楚明了)

再看稍微加強版:

n個數(shù)中有且僅有兩個數(shù)出現(xiàn)了奇數(shù)次(其它數(shù)都出現(xiàn)了偶數(shù)次),如何用線性時間常數(shù)空間找出這個數(shù)。

解法比較巧妙,主要思想是將元素分成兩組,每組中有且僅有一個數(shù)出現(xiàn)了奇數(shù)次(其它數(shù)都出現(xiàn)了偶數(shù)次),然后應用上題的結(jié)論解答,至于如何分組,見下:

1、異或所有元素得xors

2、找出xors中不為0的一個bit位(通常從右向左找出第一個不為0的)

3、以2中找到的bit位為sentinel,將數(shù)組分為兩組

本題:

思路類似于求解上題,關鍵是找出第一個來,然后借助上題結(jié)論求另外兩個

// Let s = a ^ b ^ c.? We know that s not in (a, b, c),
// since if s == a, say, then b ^ c == 0 and b == c.?
// Let f(x) be the lowest bit where x differs from s.?
// The algorithm computes flips = f(a) ^ f(b) ^ f(c),
// since the numbers appearing an even number of times cancel.?
// The variable flips has parity 1, so it is non-zero,
// and lowbit(flips) is a bit where one or three of a, b, c
// differ from s.? It can't be three, however, so the final
// exclusive-or includes exactly one of a, b, c.

代碼:

#include <iostream>

using namespace std;

// get lowest different bit
int lowbit(int x)
{
??? return x & ~(x - 1);
}

// Given an array of n integers, such that each number
// in the array appears exactly twice, except for two
// numbers (say a and b) which appear exactly once.
//
// In O(n) time and O(1) space find a and b.
// e.g.
// { 2 3 3 2 4 6 4 7 8 8 }? ---> a/b = { 6 7}
void Find2(int seq[], int n, int& a, int& b)
{
??? ////XOR
??? int xors = 0;
??? for(int i = 0; i < n; i++)
??????? xors ^= seq[i];

??? ////get different bit
??? int diff = lowbit(xors);

??? ////
??? a = 0;
??? b = 0;
??? for(int i = 0; i < n; i++)
??? {
??????? if(diff & seq[i])
??????????? a ^= seq[i];
??????? else
??????????? b ^= seq[i];
??? }
}


// Given an array of n integers, such that each number
// in the array appears exactly twice, except for three
// numbers (say a, b and c) which appear exactly once.
//
// In O(n) time and O(1) space find a,b and c.
// e.g.
// { 2 3 3 2 4 6 4 7 8 8 1 }? ---> a/b = { 6 7 1}


// Let s = a ^ b ^ c.? We know that s not in (a, b, c),
// since if s == a, say, then b ^ c == 0 and b == c.?
// Let f(x) be the lowest bit where x differs from s.?
// The algorithm computes flips = f(a) ^ f(b) ^ f(c),
// since the numbers appearing an even number of times cancel.?
// The variable flips has parity 1, so it is non-zero,
// and lowbit(flips) is a bit where one or three of a, b, c
// differ from s.? It can't be three, however, so the final
// exclusive-or includes exactly one of a, b, c.

void Find3(int seq[], int n, int& a, int& b, int& c)
{
??? ////XOR
??? int xors = 0;
??? for(int i = 0; i < n; i++)
??????? xors ^= seq[i];

??? ////
??? int flips = 0;
??? for(int i = 0; i < n; i++)
??????? flips ^= lowbit(xors ^ seq[i]);
??? flips = lowbit(flips);

??? ////get one of three
??? a = 0;
??? for(int i = 0; i < n; i++)
??? {
??????? if(lowbit(seq[i] ^ xors) == flips)
??????????? a ^= seq[i];
??? }

??? ////swap a with the last element of seq
??? for(int i = 0; i < n; i++)
??? {
??????? if(a == seq[i])
??????? {
??????????? int temp = seq[i];
??????????? seq[i] = seq[n - 1];
??????????? seq[n - 1] = temp;
??????? }
??? }

??? ////call Find2() to get b and c
??? Find2(seq, n - 1, b, c);
}

int _tmain(int argc, _TCHAR* argv[])
{
??? int a,b,c;
??? int test1[] = { 2, 3, 3, 2, 4, 6, 4, 7, 8, 8 };
??? Find2(test1, 10, a, b);
??? cout<<"a = "<<a<<" , b = "<<b<<endl;

??? int test2[] = {1 , 2 , 3 };//{ 2, 3, 3, 2, 4, 6, 4, 7, 8, 8, 1 };
??? Find3(test2, 3, a, b, c);
??? cout<<"a = "<<a<<" , b = "<<b<<" , c = "<<c<<endl;
??? system("pause");
??? return 0;
}

?

本文來自CSDN博客,轉(zhuǎn)載請標明出處:http://blog.csdn.net/yysdsyl/archive/2009/06/22/4289828.aspx

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久综合伊人77777蜜臀| 亚洲午夜国产成人av电影男同| 性色av一区二区三区红粉影视| 国产精品三级视频| 久久精品日产第一区二区| 欧美专区18| 亚洲国产毛片完整版| 91久久精品网| 欧美日本免费| 性18欧美另类| 狂野欧美一区| 亚洲欧美国产日韩天堂区| 亚洲欧美日本日韩| 亚洲激情偷拍| 亚洲夜间福利| 亚洲国产综合在线| 亚洲调教视频在线观看| 在线国产精品播放| 一本久久青青| 亚洲国产成人在线视频| 亚洲无亚洲人成网站77777 | 欧美国产日韩免费| 欧美日韩一区三区四区| 老巨人导航500精品| 欧美日韩视频一区二区三区| 麻豆成人小视频| 国产精品美女久久久久av超清| 久热国产精品视频| 国产精品二区影院| 欧美国产日韩一区二区| 国产精品久久久久aaaa九色| 欧美国产视频在线| 国产伦一区二区三区色一情| 亚洲国产另类精品专区| 欧美无砖砖区免费| 欧美激情四色 | 国产精品卡一卡二卡三| 欧美成人精品一区二区| 国产欧美在线看| 亚洲精品看片| 亚洲国产精品成人综合| 久久精品国产99| 欧美亚洲三级| 国产精品国产一区二区 | 亚洲在线播放| 欧美日韩精品中文字幕| 亚洲国产精品国自产拍av秋霞| 国产一区二区无遮挡| 亚洲私人影院| 亚洲一区二区三区视频播放| 欧美久久一级| 91久久久在线| 亚洲精品欧美日韩专区| 久久综合狠狠综合久久激情| 久久久久久久高潮| 激情六月综合| 久久精品男女| 久久综合色播五月| 激情成人在线视频| 久久久久久久91| 巨乳诱惑日韩免费av| 影音先锋久久久| 久久综合九色综合网站| 老司机成人在线视频| 在线免费观看日本欧美| 久热这里只精品99re8久| 噜噜噜躁狠狠躁狠狠精品视频 | 国产亚洲激情| 久久九九国产精品| 欧美成人网在线| 日韩一级精品视频在线观看| 欧美片第1页综合| 99综合在线| 欧美中文在线观看国产| 精品69视频一区二区三区| 久久日韩粉嫩一区二区三区| 亚洲国产精品国自产拍av秋霞| 日韩午夜激情av| 国产精品乱码一区二三区小蝌蚪| 午夜精品久久一牛影视| 美女图片一区二区| 99热免费精品| 国产精品日韩二区| 久久看片网站| 亚洲精品乱码久久久久久黑人| 亚洲自拍电影| 在线观看国产精品网站| 欧美理论电影网| 午夜精品久久久| 亚洲国产91| 欧美淫片网站| 亚洲欧洲在线观看| 国产精品日韩精品| 蜜桃av一区二区在线观看| 日韩午夜在线电影| 久久综合久久美利坚合众国| 一本色道久久88综合亚洲精品ⅰ | 亚洲在线视频网站| 国模精品娜娜一二三区| 欧美激情在线狂野欧美精品| 亚洲免费人成在线视频观看| 欧美激情一区二区三区不卡| 欧美在线二区| 99视频在线精品国自产拍免费观看| 国产精品素人视频| 欧美高清视频在线| 欧美一区二区三区久久精品| 亚洲三级影院| 老色批av在线精品| 欧美一级视频免费在线观看| 一本大道久久精品懂色aⅴ| 国自产拍偷拍福利精品免费一| 欧美人妖在线观看| 免费欧美高清视频| 久久国产日本精品| 亚洲一区国产精品| 夜夜嗨av一区二区三区四季av | 欧美韩日高清| 久久婷婷国产综合国色天香| 欧美一区二区免费| 亚洲视频中文| 99re视频这里只有精品| 亚洲国产精品一区在线观看不卡| 国产视频一区二区三区在线观看| 欧美日韩久久精品| 欧美激情一区在线观看| 另类尿喷潮videofree | 亚洲欧美网站| 亚洲欧洲av一区二区| 中文在线一区| 国产精品99久久99久久久二8| 亚洲片国产一区一级在线观看| 欧美成va人片在线观看| 久久美女性网| 久久综合色影院| 免费看亚洲片| 欧美成人免费大片| 欧美高清在线一区| 欧美大片在线观看| 亚洲国产第一| 亚洲国产一区二区视频| 亚洲第一在线综合在线| 亚洲高清毛片| 亚洲精品欧美极品| 日韩一级黄色大片| 一本久久知道综合久久| 亚洲午夜视频| 欧美一级片在线播放| 欧美在线播放一区二区| 久久亚洲国产成人| 欧美.日韩.国产.一区.二区| 欧美激情在线播放| 欧美特黄一级大片| 国产乱码精品一区二区三区忘忧草| 国产欧美一区二区三区沐欲| 狠狠色狠色综合曰曰| 亚洲国产mv| 亚洲一二三级电影| 久久精品观看| 亚洲国产视频一区| 亚洲视频图片小说| 久久精品夜色噜噜亚洲a∨ | 欧美日韩一区二区三区视频| 国产精品一区二区在线| 狠狠狠色丁香婷婷综合久久五月| 亚洲激情在线激情| 亚洲免费在线看| 久久中文精品| 99re6这里只有精品视频在线观看| 亚洲欧美成人一区二区在线电影 | 亚洲日本成人网| 欧美一二区视频| 欧美精品在线一区二区| 国产午夜亚洲精品羞羞网站| 亚洲国产精品一区制服丝袜| 亚洲一区二区三区在线视频| 久久香蕉精品| 一区二区免费在线视频| 久久天天狠狠| 国产欧美在线| 一本久久综合亚洲鲁鲁| 久久青草久久| 亚洲深夜福利网站| 欧美电影免费观看高清| 国产免费成人av| 一区二区电影免费观看| 久久久综合免费视频| 9l国产精品久久久久麻豆| 蜜臀av在线播放一区二区三区| 欧美视频免费在线| 亚洲精品久久久久久久久久久| 久久成人精品视频| 一本大道久久精品懂色aⅴ| 久久手机免费观看| 国产偷国产偷亚洲高清97cao| 一本色道88久久加勒比精品 | 久久爱www久久做| 夜久久久久久| 欧美日韩国产影片| 亚洲人成77777在线观看网|