Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

476. 数字的补数(简单) - https://leetcode-cn.com/problems/number-complement/submissions/ #16

Open
tailgo opened this issue May 24, 2019 · 0 comments
Labels
阿狗 阿狗是帅哥呀

Comments

@tailgo
Copy link
Owner

tailgo commented May 24, 2019

对二进制的每个位取反,那么只要这个数跟长度相同的全为1的二进制数异或即可。那么问题就变成了求跟数字相同的二进制位数的最大值。简单数学题。

执行用时 : 76 ms, 在Number Complement的JavaScript提交中击败了96.86% 的用户
内存消耗 : 34.2 MB, 在Number Complement的JavaScript提交中击败了6.17% 的用户

/**

  • @param {number} num
  • @return {number}
    */
    var findComplement = function(num) {
    return (Math.pow(2, (~~Math.log2(num) + 1)) - 1) ^ num;
    };
@tailgo tailgo changed the title 467. 数字的补数(简单) - https://leetcode-cn.com/problems/number-complement/submissions/ 476. 数字的补数(简单) - https://leetcode-cn.com/problems/number-complement/submissions/ May 24, 2019
@tailgo tailgo added the 阿狗 阿狗是帅哥呀 label May 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
阿狗 阿狗是帅哥呀
Projects
None yet
Development

No branches or pull requests

1 participant