【世界速看料】#yyds干貨盤點(diǎn)# LeetCode程序員面試金典:整數(shù)轉(zhuǎn)換

2022-12-29 19:29:41 來源:51CTO博客


(資料圖片僅供參考)

題目:

整數(shù)轉(zhuǎn)換。編寫一個(gè)函數(shù),確定需要改變幾個(gè)位才能將整數(shù)A轉(zhuǎn)成整數(shù)B。

示例1:

輸入:A = 29 (或者0b11101), B = 15(或者0b01111) 輸出:2

示例2:

輸入:A = 1,B = 2 輸出:2

代碼實(shí)現(xiàn):

class Solution {    public int convertInteger(int A, int B) {        int temp = A ^ B;        int count = 0;        while (temp != 0) {            temp &= (temp - 1);  // 去掉二進(jìn)制表示的最右邊的1            count++;        }        return count;    }}

標(biāo)簽: 整數(shù)轉(zhuǎn)換

上一篇:今日關(guān)注:二維數(shù)組、數(shù)組指針以及指針數(shù)組
下一篇:【全球報(bào)資訊】#yyds干貨盤點(diǎn)# LeetCode程序員面試金典:下一個(gè)數(shù)