LeetCode——Number Complement
LeetCode——Number Complement
Question
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 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) {
        int i = 0;
        while (num >= pow(2, i)) {
            i++;
        }
        int value = pow(2, i > 0 ? i : 1) - 1;
        return value ^ num;
    }
};
												
											LeetCode——Number Complement的更多相关文章
- [LeetCode] Number Complement 补数
		
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
 - Leetcode: Number Complement
		
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
 - LeetCode#476 Number Complement - in Swift
		
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
 - 【leetcode】476. Number Complement
		
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
 - LeetCode_476. Number Complement
		
476. Number Complement Easy Given a positive integer, output its complement number. The complement s ...
 - 2016.5.15——leetcode:Number of 1 Bits ,
		
leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...
 - LeetCode——Number of Boomerangs
		
LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...
 - LeetCode_Easy_471:Number Complement
		
LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...
 - LeetCode 476. Number Complement (数的补数)
		
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
 
随机推荐
- tarjan求强连通分量+缩点 模板
			
#define N 100100 #define M 200200 int n,m; int id,index; //id表示缩点后点的id,index表示进行tarjan算法时访问的点先后 int ...
 - Mycat安装及测试分片总结
			
1.安装jdk1.72.连接实际mysql数据库 用命令行工具或图形化客户端,连接mysql,创建DEMO所用三个分片数据库:(默认schema.xml中的配置需要三个库) CREATE databa ...
 - 讨论cocos2d-x字体绘制原理和应用方案
			
转自:http://blog.csdn.net/langresser_king/article/details/9012789 个人一直认为,文字绘制是cocos2d-x最薄弱的环节.对于愤怒的小鸟之 ...
 - 获取当前日期和农历的js代码
			
来自:http://www.cnblogs.com/Gnepner/archive/2011/09/07/2169822.html 获取当前日期 getToday.js: function GetCu ...
 - JavaScript 学习(1)--window对象
			
JavaScript 学习--Window对象 window对象方法 1.1 创建新窗体 //窗体打开 var newWindow=Window.open("default.html&quo ...
 - junit test 报错,java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=esopCreateTest],
			
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=esopCreateTest], {ExactMatc ...
 - selectedIndex返回被选中的option的index.
			
/ <label for="city">城市</label> <select id="city" name="schoo ...
 - PyMongo的使用(转)
			
原文:http://www.oschina.net/code/snippet_1382328_37407 #!/usr/bin/env python #coding:utf-8 # Author: - ...
 - Andrew Ng机器学习编程作业:Support Vector Machines
			
作业: machine-learning-ex6 1. 支持向量机(Support Vector Machines) 在这节,我们将使用支持向量机来处理二维数据.通过实验将会帮助我们获得一个直观感受S ...
 - Angular学习笔记—基础(转载)
			
创建简单组件 新建组件 $ ng generate component simple-form --inline-template --inline-style # Or $ ng g c simpl ...