Re: [閒聊] 每日leetcode

作者: smart0eddie (smart0eddie)   2024-08-22 11:13:30
2024-08-22
476. Number Complement
The complement of an integer is the integer you get when you flip all the 0's
to 1's and all the 1's to 0's in its binary representation.
For example, The integer 5 is "101" in binary and its complement is "010"
which is the integer 2.
Given an integer num, return its complement.
為什麼我覺得好像時間跳了好幾天
泥板都在數學姊 只剩我只會暴力解了
class Solution {
public:
int findComplement(int num) {
int result = 0;
int bit = 1;
while (num >= 2) {
result += bit * (1 - (num % 2));
bit *= 2;
num /= 2;
}
return result;
}
};

Links booklink

Contact Us: admin [ a t ] ucptt.com