LeetCode_476. Number Complement
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.
package leetcode.easy;
public class NumberComplement {
public int findComplement(int num) {
int complement = 0;
int pos = 0;
while (num > 0) {
if ((num & 1) == 0) {
complement |= (1 << pos);
}
pos++;
num >>>= 1;
}
return complement;
}
@org.junit.Test
public void test() {
System.out.println(findComplement(5));
System.out.println(findComplement(1));
}
}
LeetCode_476. Number Complement的更多相关文章
- 【leetcode】476. Number Complement
problem 476. Number Complement solution1: class Solution { public: int findComplement(int num) { //正 ...
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
- LeetCode_Easy_471:Number Complement
LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...
- 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 bi ...
- 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 (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
随机推荐
- java多线程的几种实现方式
java多线程的几种实现方式 1.继承Thread类,重写run方法2.实现Runnable接口,重写run方法,实现Runnable接口的实现类的实例对象作为Thread构造函数的target3.通 ...
- spark读写Oracle、hive的艰辛之路(一)
前两天工作需求,要通过给的几个Oracle的视图把数据入到hive库中,很遗憾,使用的华为云平台的集区环境中并没有sqoop1,当然也并没有sqoop2,所以,想到的解决方案是使用spark读取Ora ...
- Hive修改表语句
0x01:重命名表 1 ALTER TABLE table_name RENAME TO new_table_name; 上面这个命令可以重命名表,数据所在的位置和分区都没有改变. 0x02:改变列名 ...
- PureComponent & shouldComponentUpdate
Called to determine whether the change in props and state should trigger a re-render. Component alwa ...
- spring boot是一个应用框架生成工具?
spring boot是一个应用框架生成工具?
- swift与oc的关系
swift是对oc的扩展 Swift是没有消息机制的Objective-C https://www.oschina.net/translate/inside-swift: swift保持了oc的类结构 ...
- webpack资源管理
一.概况 ①webpack不仅可以打包JavaScript模块,甚至它把网页开发中的一切资源的都可以当作模块来打包处理 ②但是webpack本身不支持,它只是一个打包平台,其他资源,例如css.les ...
- Linux 系统管理——系统安全及应用
chagen -d 0 ____用户名:下次登录时必须修改密码 ctrl+R:查看历史记录 history:查看历史记录 清除历史记录: >.bash _history echo“”>.b ...
- A*G#C001
AGC001 A BBQ Easy 贪心. https://agc001.contest.atcoder.jp/submissions/7856034 B Mysterious Light 很nb这个 ...
- javascript获取手机上媒体设备,摄像头和话筒
主要运用H5的媒体接口API,MDN文档: navigator.mediaDevices(新版API),Navigator.getUserMedia(旧版API,不建议使用,但某些浏览器还支持),本文 ...