[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:

  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.
class Solution {
    public:
        int findComplement(int num) {
            long int flag = 1;
            long int res = 0;
            while(flag && flag <= num){
                int t = flag&num;
                if(t == 0){
                    res |= flag;
                }

                flag = flag << 1;
            }
            return res;
        }
    };

原文地址:https://www.cnblogs.com/chenhan05/p/8280237.html

时间: 2024-11-16 05:01:49

[leetcode]Bit Manipulation-476. Number Complement的相关文章

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解题思路: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 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 476. Number Complement(easy难度c++)

题目: 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 leadin

476. Number Complement【位运算】

2017/3/14 15:36:44 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 a

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第一刷_Valid Number

又是那种看上去非常简单,但非常难过的问题,主要是所有的测试用例很难考虑全面.下面我列举出所有的情况: 1. 字符串前面是可以含有空格的,但是全部都是空格是不行的. 2. 一个数字开头可以是哪些字符呢?很容易想到的是"+"和"-",但是别忘记还有小数点. 3. 一个数字中间也是可以含有字符的,比如科学计数法的"e",而且e之后是可以接"+"或者"-"的.但是要注意e是不是作为数字的开头或者结尾的. 4. 一个

leetcode第一刷_Single Number II

其他出现两次,只有一个出现一次的那道题我就不更了,直接抑或,最后的结果就是那个数.为什么可以这样做呢?因为一个32位int,如果所有数都出现了两次,那么为1的那些位统计的个数一定是2的倍数,抑或之后全变成0.一个数出现了一次,它为1的那些位上,1的个数必定是奇数,抑或之后一定还是1. 我之前知道出现两次这个题的解法,但是理解的不够深,以为抑或是关键,其实不是,出现了偶数次才是关键.理解了这点,推广到出现3次上,如果所有的出现了三次,那么为1的那些位1的个数一定是三的倍数,那如果有一个数出现了一次

Leetcode 位运算 Single Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Single Number Total Accepted: 20063 Total Submissions: 44658 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find >the two elements that a