• <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>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開(kāi)心的工作
            簡(jiǎn)單、開(kāi)放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(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個(gè)數(shù)中有且僅有一個(gè)數(shù)出現(xiàn)了奇數(shù)次(其它數(shù)都出現(xiàn)了偶數(shù)次),如何用線性時(shí)間常數(shù)空間找出這個(gè)數(shù)。

            解法如下:

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

            再看稍微加強(qiáng)版:

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

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

            1、異或所有元素得xors

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

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

            本題:

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

            // 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;
            }

            ?

            本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/yysdsyl/archive/2009/06/22/4289828.aspx

            精品无码久久久久久尤物| 欧美午夜A∨大片久久| 久久国产高潮流白浆免费观看| 精品久久香蕉国产线看观看亚洲| 久久精品人人做人人爽97| 久久99国产亚洲高清观看首页| 精品无码人妻久久久久久 | 国产精品禁18久久久夂久| 久久免费视频观看| 久久综合久久美利坚合众国| 久久精品国产亚洲沈樵| 深夜久久AAAAA级毛片免费看| 久久精品一本到99热免费| 久久婷婷五月综合色99啪ak| 91精品国产综合久久精品| 伊人情人综合成人久久网小说| 狠狠干狠狠久久| 老男人久久青草av高清| 97久久超碰国产精品2021| 伊人久久大香线蕉综合热线| 欧美亚洲国产精品久久蜜芽 | 久久激情五月丁香伊人| 久久久无码人妻精品无码| 亚洲国产日韩欧美综合久久| 天天综合久久一二三区| 国产欧美久久一区二区| 99久久夜色精品国产网站| 亚洲精品WWW久久久久久 | 久久成人精品视频| 久久天天躁狠狠躁夜夜网站| 亚洲精品乱码久久久久久中文字幕| 久久久青草青青国产亚洲免观| 久久综合狠狠色综合伊人| 国产精品9999久久久久| MM131亚洲国产美女久久| 少妇久久久久久被弄高潮| 亚洲精品高清国产一线久久| 久久久久久国产精品免费无码| 久久久久人妻一区精品色| 精品人妻久久久久久888| 好久久免费视频高清|