• <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.¢%

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

            題目描述:

            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個數中有且僅有一個數出現了奇數次(其它數都出現了偶數次),如何用線性時間常數空間找出這個數。

            解法如下:

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

            再看稍微加強版:

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

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

            1、異或所有元素得xors

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

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

            本題:

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

            // 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博客,轉載請標明出處:http://blog.csdn.net/yysdsyl/archive/2009/06/22/4289828.aspx

            99久久中文字幕| 伊人色综合九久久天天蜜桃| 一本久久久久久久| 91精品国产综合久久精品| 久久国产高潮流白浆免费观看| 国产精品99久久精品| 精品国产乱码久久久久久郑州公司 | 国产亚洲精品美女久久久| 狠色狠色狠狠色综合久久 | 国产精品99久久久久久宅男小说| 欧美与黑人午夜性猛交久久久| 亚洲国产精品无码久久久不卡| 久久精品国产半推半就| 久久精品亚洲乱码伦伦中文| 久久偷看各类wc女厕嘘嘘| 亚洲国产成人精品久久久国产成人一区二区三区综 | 狠狠色丁香久久婷婷综合| 99久久人妻无码精品系列| 99久久婷婷国产一区二区| 久久久这里有精品| 久久国产色AV免费观看| 久久国产香蕉视频| 久久综合中文字幕| 精品国产VA久久久久久久冰| 精品视频久久久久| 久久99精品久久久久久水蜜桃| 亚洲精品乱码久久久久久 | 久久久久四虎国产精品| 中文字幕久久亚洲一区| 国产99久久久久久免费看| 人妻少妇久久中文字幕| 久久福利片| 国产激情久久久久影院老熟女免费| 亚洲va中文字幕无码久久| 无码精品久久一区二区三区| 久久久久亚洲精品男人的天堂| 亚洲午夜精品久久久久久浪潮 | 久久e热在这里只有国产中文精品99| 亚洲午夜久久久久久噜噜噜| 亚洲人成无码www久久久| 久久精品成人免费观看97|