Every non-negative integer N has a binary representation.  For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on.  Note that except for N = 0, there are no leading zeroes in any binary representation.

The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1.  For example, the complement of "101" in binary is "010" in binary.

For a given number N in base-10, return the complement of it's binary representation as a base-10 integer.

Example 1:

Input: 5
Output: 2
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.

Example 2:

Input: 7
Output: 0
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.

Example 3:

Input: 10
Output: 5
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.

Note:

  1. 0 <= N < 10^9

题意啥的去看看中文版的就好了。简单一点看看example

大佬的代码很短的

class Solution {
public:
int bitwiseComplement(int N) {
int num[] = {};
int cnt = ;
int sum = ;
int flag = ;
if(N == ){
return ;
}
while(N){
int pos = N % ;
num[cnt++] = pos == ? : ;
N/=;
}
for(int i = ; i < cnt ; i++){
//cout<<num[i]<<endl;
if(num[i] == ){
sum += ;
}else{
sum += flag;
}
flag *= ;
}
//cout<<endl;
return sum;
}
};

128th LeetCode Weekly Contest Complement of Base 10 Integer的更多相关文章

  1. 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【leetcode】1012. Complement of Base 10 Integer

    题目如下: Every non-negative integer N has a binary representation.  For example, 5 can be represented a ...

  3. #Leetcode# 1009. Complement of Base 10 Integer

    https://leetcode.com/problems/complement-of-base-10-integer/ Every non-negative integer N has a bina ...

  4. LeetCode.1009-十进制数的补码(Complement of Base 10 Integer)

    这是小川的第377次更新,第404篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第238题(顺位题号是1009).每个非负整数N都具有二进制表示.例如,5可以二进制表示为 ...

  5. LeetCode 1012 Complement of Base 10 Integer 解题报告

    题目要求 Every non-negative integer N has a binary representation.  For example, 5 can be represented as ...

  6. 128th LeetCode Weekly Contest Capacity To Ship Packages Within D Days

    A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...

  7. 128th LeetCode Weekly Contest Pairs of Songs With Total Durations Divisible by 60

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  8. [Swift]LeetCode1009. 十进制整数的补码 | Complement of Base 10 Integer

    Every non-negative integer N has a binary representation.  For example, 5 can be represented as &quo ...

  9. 123th LeetCode Weekly Contest Add to Array-Form of Integer

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...

随机推荐

  1. redis 通配符 批量删除key

    Redis 中 DEL指令支持多个key作为参数进行删除 但不支持通配符,无法通过通配符批量删除key,不过我们可以借助 Linux 的管道和 xargs 指令来完成这个动作. 比如要删除所有以use ...

  2. C# 操作计算机用户权限

    我们可以用代码来获取当前登录用户的权限信息,    用户角色类型有以下几种: // 摘要:    //     指定与 System.Security.Principal.WindowsPrincip ...

  3. C++11中的tuple应用:让函数返回多个值

    在没有tuple之前,如果函数需要返回多个值,则必须定义一个结构体,有了C++11,可以基于tuple直接做了,下面是个示例: // 编译:g++ -std=c++11 -g -o x x.cpp # ...

  4. C# 单例模式(Singleton)

    摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就是我们今天要 ...

  5. delphi 中封装的VCl窗体Tab键响应问题

    在DLL中的子窗体不会响应Tab按键的,这个时候就需要手动去指定Tab键的操作,但是前提是主窗体要向这个窗体发送一个消息,一个Tab键按下的消息.基本顺序是这样的: 1. 主窗体用Hook技术捕获Ta ...

  6. delphi中sql实现while循环插入,不存在则插入

    ' declare @i int; '+ ' set @i=0; '+ ' while @i<4 '+ ' begin '+ ' insert into NBCommission(Type,It ...

  7. CLion编译的exe文件无法在windows下正常运行

    The program cannot start because libgcc_s_dw2-1.dll is missing from your computer. Try reinstalling ...

  8. 关于.net和java des加密

    在.net和java环境中对于des加密,有几点要协同的地方: 密钥和密钥向量Key IV 加密模式CipherMode 填充模式PaddingMode 密文编码方式 下面一段.net的des加密方式 ...

  9. centos 安装erlang rpm包互相依赖问题

    在项目中使用 centos 6.5 mini 版本(网络隔离,无法上外网),因测试需要使用到 erlang 环境. erlang rpm 包下载地址:https://www.erlang-soluti ...

  10. SQLServer客户端连接工具(支持2000,20005,2008)

    绿色版本, 体积小(不到2M), 支持数据库版本2000 2005 2008 界面仿最经典的SQLServer2000: 下载地址:http://download.csdn.net/detail/gg ...