1. 程式人生 > >leetcode-476

leetcode-476

是個 etc style leetcode 多少 col 個數 一個 mov

 int findComplement(int num) {
        int move=num;
        mo|=move>>1;
        mo|=move>>2;
        mo|=move>>4;
        mo|=move>>8;
        mo|=move>>16;
        mo|=move>>32;
        return num^move;
    }

提交後時間很尷尬,12ms,不確定這個int是多少位的所以直接給了個32位。

說下思路,一個數與它有效位全為1的數異或就能得到它的補數。所以得到全為1數是個關鍵,從有效位最高位向後一直移位就能得到全為1的數,按照int位向後移位就好了。

leetcode-476