LeetCode(476): Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

解析:这个题的意思就是求出给出数据num的反码的十进制数。解题思路:num-->二进制num-->二进制num的反码-->二进制num反码的十进制表示。首先把num转换为二进制字符串,然后遍历该字符串。按题目要求,应该从二进制最左边的1开始。如010就把左边的0去掉变为10。我们可以设置一个flag,在遍历字符串时只要碰到1就把flag置为true,即开始取反码。取反码的过程就比较简单了,只要1-->0 0-->1就可以了。得到反码后再通过integer的内置方法转为十进制就可以了。  
public class Solution {
    public static int findComplement(int num) {
        StringBuilder sbin=new StringBuilder(Integer.toBinaryString(num));
        boolean flag = false;
        for (int i = 0; i < sbin.length(); i++) {
            if (sbin.charAt(i) == ‘1‘){
                flag = true;
            }
            if (flag) {
                sbin.setCharAt(i, sbin.charAt(i) == ‘1‘ ? ‘0‘ : ‘1‘);
            }
        }
        return Integer.valueOf(sbin.toString(), 2);
    }
}

注意:更改字符串是不能用String类型的,因为String在实现的时候是被设计为final类型的。如果想改变应该用StringBuilder或者StringBuffer。

一个经典的面试题是问String、StringBuilder、StringBuffer的区别。很多人在准备面试的时候估计背过好几遍却不知道究竟有什么用处,毕竟平时写代码用String就够了呀。上述例子也许可以稍微打消一点这种疑问了,String的不可变的特性导致我们无法简单地实现需要的代码逻辑(当然,用String也是可以实现的)。此时StringBuilder和StringBuffer就派上用场了。

时间: 2024-11-08 15:46:24

LeetCode(476): Number Complement的相关文章

[leetcode]Bit Manipulation-476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading ze

LeetCode OJ:Number of 1 Bits(比特1的位数)

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

LeetCode 191:number of one bits

题目就是: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function shoul

LeetCode OJ:Number of Islands(孤岛计数)

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

LeetCode#476 Number Complement - in Swift

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. 給一個正整數,算出他的二補數 (2's complement). 二補數就是將該數字的二進制碼全部翻轉過來. Note: The given integer is guaranteed to fit within the range of

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen

【LeetCode】Single Number

原文: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl