Re: [閒聊] 每日leetcode
看板Marginalman作者smart0eddie (smart0eddie)時間1年前 (2024/08/22 11:13)推噓0(0推 0噓 0→)留言0則, 0人參與討論串753/1548 (看更多)
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;
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 73.173.211.221 (美國)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1724296412.A.3BF.html
討論串 (同標題文章)
完整討論串 (本文為第 753 之 1548 篇):