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