2022年6月8日 星期三

[693] Binary Number with Alternating Bits

因為被DP跟sliding window 荼毒了兩天還寫不出來, 決定先寫個easy回覆一下自信XD
結果第一次submit 就過了!真是感人XD~~~~~看了一下討論區好像也沒什麼可以改進的, 
就這樣吧姆襪哈哈哈哈哈哈~~~~~~(有種騙題數的感覺XD)
L
bool hasAlternatingBits(int n){
    bool lastBit,newBit;
    lastBit = n&(0x1);
    while (n>0)
    {
        n=n>>1;
        newBit=n&(0x1);
        if (lastBit ==newBit)
            return false;
        else
            lastBit = newBit;
    }
    return true;       
}

沒有留言:

張貼留言