260. Single Number III

My Submissions

Total Accepted: 49927
Total Submissions: 104463
Difficulty: Medium

Given an array of numbers nums
, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5]
, return [3, 5]
.
Note:
The order of the result is not important. So in the above example, [5, 3]
is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

**/*
  * 假定這兩數(shù)為a和b窖维。做法分兩步走搪哪,第一步用xor 求出a和b的xor -diff晕鹊,然后求diff & 它的二補數(shù),得到a和b的不同位清女。
  接著把數(shù)組根據(jù) diff & num == 0 分成兩個group,分別對兩個group進行xor,得到最終a和b
  
  * In the first pass, we XOR all elements in the array, and get the XOR of
  * the two numbers we need to find. Note that since the two numbers are
  * distinct, so there must be a set bit (that is, the bit with value '1') in
  * the XOR result. Find out an arbitrary set bit (for example, the rightmost
  * set bit).
  * 
  * In the second pass, we divide all numbers into two groups, one with the
  * aforementioned bit set, another with the aforementinoed bit unset. Two
  * different numbers we need to find must fall into the two distrinct
  * groups. XOR numbers in each group, we can find a number in either group.
  * 
  * Complexity:
  * 
  * Time: O (n)
  * 
  * Space: O (1)
  * 
  * A Corner Case:
  * 
  * When diff == numeric_limits<int>::min(), -diff is also
  * numeric_limits<int>::min(). Therefore, the value of diff after executing
  * diff &= -diff is still numeric_limits<int>::min(). The answer is still
  * correct.
  * https://discuss.leetcode.com/topic/21605/accepted-c-java-o-n-time-o-1-space-easy-solution-with-detail-explanations
  */
 public int[] singleNumber(int[] nums) {
  // Pass 1 :
  // Get the XOR of the two numbers we need to find
  int diff = 0;
  for (int num : nums) {
   diff ^= num;
  }
  // System.out.println(diff);  // ( diff = 8 ^ 4 = 12,  1100)
  // Get its last set bit
  diff &= -diff;     // -diff  是diff的二補數(shù)    0011 + 1 = 0100 = 4;   12 &=4  -> 4
  // https://en.wikipedia.org/wiki/Two%27s_complement
  //System.out.println(diff);
  // Pass 2 :
  int[] rets = { 0, 0 }; // this array stores the two numbers we will
        // return
  for (int num : nums) {
   if ((num & diff) == 0) // the bit is not set
   {
    rets[0] ^= num;
   } else // the bit is set
   {
    rets[1] ^= num;
   }
  }
  return rets;
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末塘幅,一起剝皮案震驚了整個濱河市营曼,隨后出現(xiàn)的幾起案子乒验,更是在濱河造成了極大的恐慌,老刑警劉巖蒂阱,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锻全,死亡現(xiàn)場離奇詭異,居然都是意外死亡录煤,警方通過查閱死者的電腦和手機鳄厌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妈踊,“玉大人了嚎,你說我怎么就攤上這事∠煳” “怎么了新思?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長赘风。 經(jīng)常有香客問我夹囚,道長,這世上最難降的妖魔是什么邀窃? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任荸哟,我火速辦了婚禮假哎,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鞍历。我一直安慰自己舵抹,他們只是感情好,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布劣砍。 她就那樣靜靜地躺著惧蛹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪刑枝。 梳的紋絲不亂的頭發(fā)上香嗓,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音装畅,去河邊找鬼靠娱。 笑死,一個胖子當著我的面吹牛掠兄,可吹牛的內(nèi)容都是我干的像云。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蚂夕,長吁一口氣:“原來是場噩夢啊……” “哼迅诬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起双抽,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤百框,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后牍汹,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铐维,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年慎菲,在試婚紗的時候發(fā)現(xiàn)自己被綠了嫁蛇。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡露该,死狀恐怖睬棚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情解幼,我是刑警寧澤抑党,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站撵摆,受9級特大地震影響底靠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜特铝,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一暑中、第九天 我趴在偏房一處隱蔽的房頂上張望壹瘟。 院中可真熱鬧,春花似錦鳄逾、人聲如沸稻轨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽殴俱。三九已至,卻和暖如春请琳,著一層夾襖步出監(jiān)牢的瞬間粱挡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工俄精, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人榕堰。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓竖慧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親逆屡。 傳聞我的和親對象是個殘疾皇子圾旨,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容