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 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. - 思路:给定一个正整数,输出它的补数。补数的策略是通过翻转二进位表示。
- 1.求出该数的二进制位数;
2.通过给定数num和相同位数的二进制数(全为1)进行异或,即可求出补数。 public int findComplement(int num) { int ans = 0;//the count of the num's bits
int temp = num;//copy of the num
while(temp != 0){
ans++;
temp /= 2;
}
return num ^ (int)(Math.pow(2,ans)-1);
}pow() 函数用来求 x 的 y 次幂(次方),其原型为:
double pow(double x, double y);
476. Number Complement(补数)的更多相关文章
- 【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 数字的补数
给定一个正整数,输出它的补数.补数是对该数的二进制表示取反.注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位.示例 1:输入: 5输出: 2解释: 5的 ...
- [LeetCode] 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 ...
随机推荐
- 最长递增子序列 (LIS) Longest Increasing Subsequence
问题描述: 有一个长为n的数列a0, a1,..., an-1.请求出这个序列中最长的上升子序列.请求出这个序列中最长的上升子序列. 上升子序列:对于任意i<j都满足ai<aj的子序列. ...
- [Vijos] 河蟹王国
描述 河蟹王国有一位河蟹国王,他的名字叫羊驼.河蟹王国富饶安定,人们和谐相处.有一天,羊驼国王心血来潮,想在一部分人中挑出最和谐的人.于是,羊驼国王将他的子民排成了一列(==!!b汗~好长呀).每个人 ...
- BZOJ1710: [Usaco2007 Open]Cheappal 廉价回文
len<=2000的字符串上,给出删掉和添加每种字符的花费,求把字符串变成回文串的最小花费. 首先每个字符添加和删除是一样的,因此花费在添加和删掉每个字符的花费中取小的. 如果每个字符的花费都是 ...
- BZOJ 4894 有向图 外向生成树个数
4894: 天赋 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 191 Solved: 150[Submit][Status][Discuss] D ...
- HUNAN 11567 Escaping (最大流)
http://acm.hunnu.edu.cn/online/?action=problem&type=list&courseid=0&querytext=&pagen ...
- [bzoj4199][Noi2015]品酒大会_后缀自动机_后缀树_树形dp
品酒大会 bzoj-4199 Noi-2015 题目大意:给定一个字符串,如果其两个子串的前$r$个字符相等,那么称这两个子串的开头两个位置$r$相似.如果两个位置勾兑在一起那么美味度为两个位置的乘积 ...
- [Bzoj1015][JSOI2008]星球大战starwar(并查集)(离线处理)
1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 6849 Solved: 3204[Submit ...
- Entity framework自定义字段实现思路
ublic class MyModel { public int MyModelID { get; set; } public string FixedProperty1 { get; set; } ...
- Maven查看依赖树
1.命令行,但是只能查看一层的引用 mvn dependency:tree 如图所示: 2.使用Eclipse的Dependency Hierarchy查看,这个就比较深层次的查看,如图所示: 3.使 ...
- Openwrt 安装软件到U盘或硬盘
http://blog.licess.org/openwrt-install-software-to-udisk-harddisk/ 运行一个多月的DDNAS被结婚来玩的小孩给关了,于是趁机更新了一下 ...