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

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ù)次),然后應(yīng)用上題的結(jié)論解答,至于如何分組,見下:

1、異或所有元素得xors

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

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

本題:

思路類似于求解上題,關(guān)鍵是找出第一個來,然后借助上題結(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)載請標(biāo)明出處: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>
            亚洲一区二区动漫| 欧美一站二站| 亚洲国产一二三| 欧美freesex8一10精品| 亚洲三级免费| 99riav1国产精品视频| 欧美午夜视频在线| 亚洲欧美一区二区在线观看| 亚欧成人在线| 亚洲电影第1页| 亚洲精品免费电影| 国产精品国产三级国产专区53 | 久久精品免费电影| 精品91久久久久| 亚洲激情自拍| 国产欧美一区二区三区国产幕精品| 久久精品首页| 欧美国产极速在线| 亚洲欧美中文字幕| 久久久久国产精品一区| 日韩一级视频免费观看在线| 一本大道久久a久久精品综合| 国产日韩欧美电影在线观看| 欧美黄色日本| 国产伦精品一区二区三区免费迷| 男女av一区三区二区色多| 欧美高清在线观看| 午夜视频久久久| 毛片基地黄久久久久久天堂| 亚洲欧美日韩国产一区| 老司机67194精品线观看| 亚洲欧美国产日韩中文字幕| 麻豆亚洲精品| 久久成人在线| 欧美色一级片| 欧美黄色aaaa| 一区二区视频在线观看| 中日韩美女免费视频网址在线观看 | 国产精品久久久久久久久免费樱桃| 久久高清一区| 欧美视频专区一二在线观看| 女人天堂亚洲aⅴ在线观看| 国产精品劲爆视频| 亚洲黄网站黄| 在线欧美亚洲| 久久精品国产清自在天天线| 亚洲专区一区二区三区| 六月天综合网| 久久亚洲精品视频| 国产精品美女久久久久av超清 | 欧美成人精精品一区二区频| 国产精品色婷婷| 999在线观看精品免费不卡网站| 伊伊综合在线| 久久久www成人免费精品| 欧美一级在线播放| 国产精品久久久久久久久久免费看| 亚洲国产精品福利| 狠狠入ady亚洲精品经典电影| 亚洲图片你懂的| 亚洲女同在线| 国产精品久久久免费| 99精品国产在热久久下载| 亚洲人成人99网站| 欧美成人四级电影| 亚洲第一网站| 99国产精品99久久久久久粉嫩| 欧美xart系列高清| 亚洲东热激情| 亚洲精品一区二区在线观看| 欧美大片免费| av成人免费观看| 亚洲综合视频网| 国产精品欧美在线| 欧美一区二区在线免费观看| 噜噜噜91成人网| 亚洲国产另类 国产精品国产免费| 欧美在线观看天堂一区二区三区| 久久精品国产成人| 激情久久综艺| 欧美成人午夜剧场免费观看| 亚洲精品国产日韩| 亚洲欧美日韩视频一区| 国产日韩欧美综合一区| 欧美中文字幕久久| 欧美国产大片| 亚洲视频在线观看| 国产一区99| 欧美+日本+国产+在线a∨观看| 最近中文字幕mv在线一区二区三区四区| 一本色道久久99精品综合| 欧美四级电影网站| 久久爱www久久做| 亚洲三级免费观看| 亚洲欧美日韩天堂一区二区| 国产真实久久| 欧美日韩国产一区精品一区| 亚洲小说区图片区| 欧美电影在线播放| 亚洲欧美精品在线| 亚洲国产高清一区二区三区| 国产精品国产三级国产专播精品人| 欧美一区二区成人6969| 亚洲国产一区二区三区在线播| 亚洲女女女同性video| 亚洲高清久久| 国产日本欧美视频| 欧美精品一区二区三| 亚洲欧美日韩国产综合精品二区| 欧美电影免费观看网站| 午夜精品久久一牛影视| 亚洲精品麻豆| 狠久久av成人天堂| 国产精品高精视频免费| 久久在线免费视频| 性色av一区二区三区| 亚洲精品一区在线观看| 蜜桃精品久久久久久久免费影院| 亚洲一区精彩视频| 亚洲经典在线| 国内外成人免费激情在线视频网站| 欧美日韩国产美| 久久人人爽人人| 欧美一区二区黄| 亚洲综合日本| 亚洲视频免费在线| 亚洲伦理在线观看| 亚洲高清网站| 暖暖成人免费视频| 久久久久久久91| 午夜影院日韩| 亚洲欧美在线磁力| 亚洲欧美日韩精品久久久| 一本色道久久综合精品竹菊 | 国产精品三上| 欧美亚一区二区| 欧美日韩亚洲一区三区| 欧美大片91| 欧美国产一区二区三区激情无套| 久久嫩草精品久久久久| 久久九九99| 久久亚洲捆绑美女| 久久综合久久综合这里只有精品| 欧美一区高清| 久久久亚洲国产美女国产盗摄| 欧美在线看片a免费观看| 欧美在线日韩精品| 欧美在线在线| 久久影音先锋| 欧美电影免费观看网站| 欧美精品www| 欧美色123| 国产精品免费区二区三区观看| 国产精品亚洲综合色区韩国| 国产欧美亚洲日本| 国外成人在线| 亚洲精品国久久99热| 99re66热这里只有精品4| 日韩一二三区视频| 亚洲免费一在线| 久久精品盗摄| 欧美国产日产韩国视频| 亚洲精品国产精品乱码不99| 99视频在线精品国自产拍免费观看 | 亚洲一区中文| 久久精品国产在热久久| 欧美成人激情在线| 亚洲美女黄色| 欧美专区日韩视频| 欧美成人日本| 国产欧美一区二区精品秋霞影院| 韩国女主播一区| 99视频有精品| 久久国产精品99精品国产| 免费欧美日韩国产三级电影| 亚洲激情视频网站| 亚洲欧美日韩国产另类专区| 葵司免费一区二区三区四区五区| 欧美日韩在线免费视频| 国产午夜精品美女视频明星a级| 亚洲欧洲另类国产综合| 亚洲欧美影音先锋| 欧美激情第3页| 亚洲欧美激情诱惑| 欧美成人一二三| 国产亚洲欧美一区二区| 一区二区三区 在线观看视| 欧美在线观看视频一区二区三区| 亚洲国产精选| 欧美一级淫片播放口| 欧美欧美全黄| 在线播放不卡| 欧美在线在线| 亚洲精品一级| 美女视频黄a大片欧美| 国产欧美精品日韩精品| 99国产精品99久久久久久粉嫩| 久久裸体艺术| 亚洲欧美日韩一区二区在线| 欧美日韩国产a|