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 ...
随机推荐
- OC-Xcode中导入runtime框架,函数参数没有提示的处理方法
在了解runtime时,如果自己编写runtime代码,需要先导入头文件: #import <objc/message.h> 之后,例如了解runtime的消息机制时,调用objc_msg ...
- C. Day at the Beach---cf559
http://codeforces.com/problemset/problem/599/C 题目大意: 有n个城堡的高度 让你最多分成几个块 每个块排过序之后 整体是按照升序来的 分析: ...
- CF723E(欧拉回路)
题意: 给出一个有向图,要求给每条边重定向,使得定向后出度等于入度的点最多,输出答案和任意一种方案. 分析: 将图看作无向图,对每条边重定向 首先我们肯定分成多个连通分量来考虑,每一个连通分量都是一个 ...
- java 数组 输入5名学生的成绩 得出平均分。
import java.util.Scanner; public class LianXi4{ public static void main(String[] args){ //创建长度为5的数组 ...
- 扫描控件Web在线Applet
基于JAVAEE的B/S架构由于java语言的跨平台性 所以操控Window客户端资源能力有限, 目前比较流行是用其他语言如Delphi,VB,C++开发客户端控件 然后再html中用js调用. ...
- "What's New" WebPart in SharePoint
"What's New" WebPart in SharePoint 项目描写叙述 这是一个自己定义WebPart,能够显示一个列表,这个列表项目是在SharePo ...
- Oracle冷备和热备脚本
Oracle冷备和热备脚本 冷备脚本: set feedback off set heading off set verify off set trimspool off set echo off ...
- 盘点UML中的四种关系
生活中,我们既是独立的个体,又通过联系形成各种关系,比方说:朋友.恋人.父子,同学--于是乎,出现了神乎其神的六人定律. 那么在UML中又存在什么样的关系呢?以下我们来梳理一下. 关联(Associa ...
- MyBatis -- sql映射文件具体解释
MyBatis 真正的力量是在映射语句中. 和对等功能的jdbc来比价,映射文件节省非常多的代码量. MyBatis的构建就是聚焦于sql的. sql映射文件有例如以下几个顶级元素:(按顺序) cac ...
- 开源yYmVc项目,邀您和我一起开发:)
打算在闲暇时间写个MVC框架,要有什么功能一步一步边写边加,仿照struts 2 和 spring mvc.假设您感兴趣的话,能够私密我,给您加入key:). 欢迎您的到来~ 项目放在基于GIT的CS ...