476 Number Complement 数字的补数
给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。
注意:
给定的整数保证在32位带符号整数的范围内。
你可以假定二进制数不包含前导零位。
示例 1:
输入: 5
输出: 2
解释: 5的二进制表示为101(没有前导零位),其补数为010。所以你需要输出2。
示例 2:
输入: 1
输出: 0
解释: 1的二进制表示为1(没有前导零位),其补数为0。所以你需要输出0。
详见:https://leetcode.com/problems/number-complement/description/
C++:
class Solution {
public:
int findComplement(int num) {
bool start=false;
for(int i=31;i>=0;--i)
{
if(num&(1<<i))
{
start=true;
}
if(start)
{
num^=(1<<i);
}
}
return num;
}
};
参考:http://www.cnblogs.com/grandyang/p/6275742.html
476 Number Complement 数字的补数的更多相关文章
- Leetcode476.Number Complement数字的补数
给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解释: 5的二 ...
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- 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 ...
- 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 ...
- 476. Number Complement
题目 Given a positive integer, output its complement number. The complement strategy is to flip the bi ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- LeetCode 476 Number Complement 解题报告
题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...
随机推荐
- fstab文件解析
1 这个文件的用途 这个文件是启动时自动挂载指定的磁盘或者分区到系统目录下用的,提供给mount命令用. 2 文件解析 每一行是一次mount操作. 磁盘或者分区 挂载的目录 挂载的磁盘 ...
- 使用Microsoft Office 2007将文档转换为PDF
点击帮助 输入关键词PDF后搜索 点击进入Save or convert to PDF or XPS 点击进入2007 Microsoft Office Add-in: Microsoft Save ...
- bfs 邻接表
#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { int date; st ...
- POJ3579 Median —— 二分
题目链接:http://poj.org/problem?id=3579 Median Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
- oracle自动统计信息
在11g中,默认自动收集统计信息的时间为晚上10点(周一到周五,4个小时),早上6点(周六,周日,20个小时), select a.window_name, a.repeat_interval,a ...
- 微信小程序开发之https从无到有
本篇不讲什么是https,什么是SSL,什么是nginx 想了解这些的请绕道,相信有很多优秀的文章会告诉你. 本篇要讲的在最短的时间内,让你的网站从http升级到https. 开始教程前再说一句:ht ...
- PYTHON XPath与lxml类库
XPath,我们可以用先将HTML文档转换成XML文档,然后用XPath查找HTML节点或元素. XML文档实例 HTML DOM模型示例 HTML DOM定义了访问和操作HTML文档的标准方法,以树 ...
- django上课笔记1-目录介绍-路由系统-ORM操作
一.Django目录介绍 django-admin startproject mysite # 创建名为mysite的项目 cd mysite # 切换到该目录下 python manage.py s ...
- 洛谷 - P1044 - 栈 - 简单dp
https://www.luogu.org/problemnew/show/P1044 由于是用标签搜索进来的,所以这道题一定是有dp的解法. 很显然规定每次加入元素之前可以从栈中清理出任意数量的元素 ...