leetcode 476、數字的補數

題目描述:

給定一個正整數,輸出它的補數。補數是對該數的二進制表示取反。

代碼:

class Solution {
    public int findComplement(int num) {
		int p = 34,ans = 0;
		int []bin = new int[35];
        while(num != 0) {
        	bin[p--] = num % 2;
        	num /= 2;
        }
        while(++p < 35) {
        	ans = ans * 2 + 1 - bin[p];
        }
        return ans;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章