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 ...
随机推荐
- 删除或关闭Word中的超链接
最近使用的word老是会把一些文字内容或者标题转换成乱七八糟的格式,看的莫名其妙的,找了好久也不知道什么问题,后来一查才知道是因为这些文字包含超链接,word自动转换了...你说是不是莫名其妙. 要关 ...
- php的选择排序
往前. <?php /** * 选择排序 * 工作原理是每次从待排序的元素中的第一个元素设置为最小值, * 遍历每一个没有排序过的元素,如果元素小于现在的最小值, * 就将这个元素设置成为最小值 ...
- JVM笔记搬迁
JVM GC方式 回收对象 引用计数算法 可达性分析算法 引用类型 监控命令 回收算法 GC收集器 分代收集 JVM HotSpot VM https://www.cnblogs.com/lfs2 ...
- 利用python画小猪佩奇
import turtle as t t.pensize(4) t.hideturtle() t.colormode(255) t.color((255,155,192),"pink&quo ...
- Python 11 提取括号中间的内容
原文:https://blog.csdn.net/your_answer/article/details/80456550 import re string = 'abe(ac)ad)' p1 = r ...
- 【题解】洛谷 P1080 国王游戏
目录 题目 思路 \(Code\) 题目 P1080 国王游戏 思路 贪心+高精度.按\(a \times b\)从小到大排序就可以了. \(Code\) #include<bits/stdc+ ...
- nginx.conf 配置解析之文件结构
nginx.conf配置文件结构如下: ...... #主要定义nginx的全局配置 events{ #events(事件)块:主要配置网络连接相关 } http{ #http块:代理缓存和日志定义绝 ...
- return、reutrn false、e.preventDefault、e.stopPropagation、e.stopImmediatePropagation的区别
return var i = function(){ return } console.log(i())//undefined return的主要作用是阻止函数继续执行,直接返回undefined r ...
- linux 挂载windows ntfs 分区 -- centos 安装ntfs-3g
安装fuse 下载: wget http://nchc.dl.sourceforge.net/project/fuse/fuse-2.X/2.9.2/fuse-2.9.2.tar.gz 安装: tar ...
- 【转】vim复制与粘贴
用vim写代码时,经常遇到这样的场景,复制多行,然后粘贴. 这样做:1. 将光标移动到要复制的文本开始的地方,按v进入可视模式.2. 将光标移动到要复制的文本的结束的地方,按y复制.此时vim会自动将 ...