LeetCode: 476 Number Complement(easy)
题目:
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:
Output:
Explanation: The binary representation of is (no leading zero bits), and its complement is . So you need to output .
代码:
class Solution {
public:
int findComplement(int num) {
int x = , p = ;
while(num){
if(num%==) x |= ( << p);
++p;
num/=;
}
return x;
}
};
LeetCode: 476 Number Complement(easy)的更多相关文章
- 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 (数的补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode 476 Number Complement 解题报告
题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- [LeetCode&Python] Problem 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- 476. Number Complement
题目 Given a positive integer, output its complement number. The complement strategy is to flip the bi ...
- 476. Number Complement 二进制中的相反对应数
[抄题]: Given a positive integer, output its complement number. The complement strategy is to flip the ...
随机推荐
- OpenKM安装(CentOS6)
OpenKM全称是Open Knowledge Management,是一个DMS(文档管理系统).本文介绍如何在CentOS下安装它.本文的安装程序和资料全部来自OpenKM官网:http://ww ...
- Cocos2d-x3.1FileUtilsTest使用
Cocos2d-x3.1中FileUtils的使用:本使用教程是基于HelloWorld的.仅仅需在HelloWorld的init()函数中加入例如以下代码 //头文件 #include " ...
- EasyDarwin开源流媒体云平台中boost Base64编解码后与源长度不匹配的bug
本文转自EasyDarwin团队Alex的博客:http://blog.csdn.net/cai6811376 EasyDarwin云平台中部分协议使用了Base64编码昨晚报文通信的载体.比如在对摄 ...
- Spring整合Struts2的方法
一.基本支持 通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器.也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中. ...
- Running several name-based web sites on a single IP address.
VirtualHost Examples - Apache HTTP Server Version 2.2 http://httpd.apache.org/docs/2.2/vhosts/exampl ...
- docker: Dockerfile命令介绍
前一章介绍了Dockerfile创建镜像的方法,Dockerfile文件都是一些指令,因此要掌握Dockerfile就必须了解这些指令.这一章就介绍下Dockerfile的指令. From: 功能为指 ...
- Strans 内表-xml文件
一.内表到xml文件 <?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transfo ...
- [zjoi]青蛙的约会_扩展欧几里德
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝着对方那里跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚 ...
- codeforces Codeforces Round #273 (Div. 2) 478B
B. Random Teams time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- LightOJ1197 Help Hanzo —— 大区间素数筛选
题目链接:https://vjudge.net/problem/LightOJ-1197 1197 - Help Hanzo PDF (English) Statistics Forum Tim ...