Leetcode - Single Number II

My code:

public class Solution {
    public int singleNumber(int[] nums) {
        if (nums == null || nums.length == 0)
            return 0;
        int[] arr = new int[32];
        int ret = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < 32; j++) {
                if (((nums[i] >> j) & 1) == 1)
                    arr[j] += 1;
            }
        }
        
        for (int i = 0; i < 32; i++) {
            if (arr[i] % 3 == 1)
                ret += (1 << i);
        }
        return ret;
    }
}

My test result:

這道題目,看了之后就決定看答案了,bit manipulation,記住怎么操作就行了。
然后 這類題目,目前還不想深入介入。
看這個(gè)博文就懂了。
http://www.cnblogs.com/springfor/p/3870863.html

**
總結(jié): bit maniupulation
明天微軟面試, 內(nèi)心其實(shí)很看重,很在乎,所以很緊張。希望好運(yùn)!
希望女朋友的托福成績可以有進(jìn)步!
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public int singleNumber(int[] nums) {
        if (nums == null || nums.length == 0)
            return -1;
        int[] count = new int[32];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < 32; j++) {
                if (((nums[i] >> j) & 1) == 1) {
                    count[j] += 1;
                    count[j] = count[j] % 3;
                }
            }
        }
        int base = 1;
        int ret = 0;
        for (int i = 0; i < 32; i++) {
            ret += count[i] * base;
            base *= 2;
        }
        return ret;
    }
}

沒怎么細(xì)想,看答案做了出來。
其實(shí)就是把每個(gè)數(shù)都拆車2為底的多塊。用一個(gè)32位數(shù)組記錄個(gè)數(shù)。然后 % 3,因?yàn)槌霈F(xiàn)三次的數(shù),他們各個(gè)部分的次數(shù)一定是3,可以全部消除。
由此可以想到,如果這個(gè)數(shù)列中,所有數(shù)字都出現(xiàn)了k次,只有一個(gè)數(shù)字只用了一次。
那么就是同樣的方法, % k
累,晚安。

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public int singleNumber(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        
        int[] bits = new int[32];
        for (int i = 0; i < nums.length; i++) {
            int temp = nums[i];
            for (int j = 31; j >= 0; j--) {
                if ((temp & 1) == 1) {
                    bits[j]++;
                }
                temp = (temp >> 1);
            }
        }
        
        int ret = 0;
        int base = 1;
        for (int i = 31; i >= 0; i--) {
            ret += (bits[i] % 3) * base;
            base *= 2;
        }
        
        return ret;
    }
}

沒想到直接做了出來。還是記得思路吧。。。

Anyway, Good luck, Richardo! 08/05/2016

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,936評(píng)論 0 33
  • Question:**Given an array of integers, every element appe...
    Richardo92閱讀 432評(píng)論 1 1
  • My code: My test result: 這次題目也是屬于基本放棄的那種。具體做法看這個(gè)博客。Bit ma...
    Richardo92閱讀 467評(píng)論 0 1
  • My code: My test result: 這次題目感覺挺簡單的,先用告訴公式算出總和,然后一個(gè)個(gè)減,失去的...
    Richardo92閱讀 379評(píng)論 0 1
  • 門縫處一只死掉的蟑螂肚皮正朝上淋雨噴頭的水有時(shí)滴答嘀嗒藍(lán)色 紅色 充電器的燈不斷變換顏色車子碾過馬路 濺起路邊的水...
    三只貓的三腳貓功夫閱讀 204評(píng)論 1 1

友情鏈接更多精彩內(nèi)容