Class Solution

java.lang.Object
g0401_0500.s0476_number_complement.Solution

public class Solution extends Object
476 - Number Complement.<p>Easy</p> <p>The <strong>complement</strong> of an integer is the integer you get when you flip all the <code>0</code>&rsquo;s to <code>1</code>&rsquo;s and all the <code>1</code>&rsquo;s to <code>0</code>&rsquo;s in its binary representation.</p> <ul> <li>For example, The integer <code>5</code> is <code>&quot;101&quot;</code> in binary and its <strong>complement</strong> is <code>&quot;010&quot;</code> which is the integer <code>2</code>.</li> </ul> <p>Given an integer <code>num</code>, return <em>its complement</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> num = 5</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> num = 1</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= num < 2<sup>31</sup></code></li> </ul> <p><strong>Note:</strong> This question is the same as 1009: <a href="https://leetcode.com/problems/complement-of-base-10-integer/" target="_top">https://leetcode.com/problems/complement-of-base-10-integer/</a></p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findComplement

      public int findComplement(int num)