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 ...
随机推荐
- 如何使用h5py读入数据
在网上找到的方法,如下: import h5py import numpy as np #HDF5的写入: imgData = np.zeros((2,4)) f = h5py.File('HDF5_ ...
- Goland在go mod vendor模式下无法识别某些库
症状:go build可以正常编译,但代码编辑器里面提示找不到相关lib,后来发现是因为go.mod中没有用require这个库,补上库地址和版本.因为项目的mod vendor模式,版本一般不需要写 ...
- IDEA连接Oracle数据库
连接Oracle的前提是导入了相关的jar或者依赖 下面是连接数据库的操作 如果出现了下面的画面,就说明连接成功
- be of + 名词
语法: “be of +抽象名词(词组)” 表示主语的某种形状或特征,相当于 "be+形容词" 例如: be of value=be valuable : be of inter ...
- Xamarin.Android开发
使用 Visual Studio 生成第一个 Xamarin.Android 应用程序,并进一步了解使用 Xamarin 进行 Android 应用程序开发的基础知识.在此过程中,会介绍生成和部署 X ...
- 使用packer 打包nodegui 应用
packer 是nodegui 团队提供的专门用来打包noodegui 应用程序的工具 安装packer yarn 方式安装 yarn add @nodegui/packer 效果 [fsevents ...
- benchmarkdotnet docker 运行
使用docker 运行基准测试是一个不错的选择,可以减少我们环境搭建的时间,同时也可以加速ci/cd 环境准备 docker-compose 文件 version: "3" ser ...
- django的惰性查询
django中的查询,在写好查询条件之后,在不调用变量的时候,sql是不会执行的,只有在调用变量的时候,才回去执行, 在一次查询之后,会把变量放进内存,下次再使用这个变量的时候就会使用内存汇总的值. ...
- 8.学习springmvc的拦截器
一.springmvc拦截器介绍和环境搭建 1.介绍: 过滤器:servlet中的一部分,可以拦截所有想要访问的资源. 拦截器:SpringMVC框架中的,只能在SpringMVC中使用并且只能过滤控 ...
- centos7安装pure-ftpd
1.获取安装包 .tar.gz && cd pure-ftpd-1.0.47 ./configure --prefix=/usr/local/pureftpd --without-in ...