Unknown's avatar

1009. Complement of Base 10 Integer

https://leetcode.com/problems/complement-of-base-10-integer/

class Solution {
    public int bitwiseComplement(int N) {
    String x = Integer.toBinaryString(N);
        StringBuilder out = new StringBuilder();
        for (int i = 0; i <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">&#65279;</span>&lt; x.length(); i++){
            if(x.charAt(i) == &#039;0&#039;)
                out.append(&quot;1&quot;);
            else out.append(&quot;0&quot;);
        }
        return Integer.parseInt(out.toString(), 2);
}}

Better Solution

Leave a comment