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. Ubuntu14.04下 安装xhprof

    1.下载xhprof包: wget http://pecl.php.net/get/xhprof-0.9.4.tgz 2.解压 进入扩展目录 .tgz cd /home/justphp/xhprof- ...

  2. javascript总结41:表格全选反选,经典案例详解

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. Linux下oracle定时备份

    1. 设置数据库空表可导出(oracel11g) 用PL/SQL登录数据库(或者其他工具) 执行: select 'alter table '||table_name||' allocate exte ...

  4. Java基本类型学习

    基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型.它们是我们编程中使用最频繁的类型.java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变量的初始化. 1. Java ...

  5. WCF服务编程 读书笔记——第1章 WCF基础(2)

    续:第1章 WCF基础(1) 元数据交换 服务有两种方案可以发布自己的元数据.一种是基于HTTP-GET协议提供元数据, 另一种则是后面将要讨论的使用专门的终结点的方式.WCF能够为服务自动提供基于H ...

  6. 编写高质量代码改善C#程序的157个建议——建议100:静态方法和实例方法没有区别

    建议100:静态方法和实例方法没有区别 静态方法在加载时机和内存使用上和实例方法完全一致.在这里,我们先引出一个概念“类型对象”.比如类型Person,我们都知道new Person() 会产生一个对 ...

  7. Android测试入门篇

    Android本身是一套软件堆叠(Software Stack),或者成为软件叠层架构,叠层主要分成三层:操作系统.中间件和应用程序. Android构架 1. Application 应用程序层:用 ...

  8. C++-结构体,联合体,枚举,的区别

    结构体: struct  NUM { int number; }a; 结构体是声明只是一个模型,没有分配内存空间.当进行定义结构体变量后才分配内存空间 联合体: union  data { int a ...

  9. jQuery制作多表格固定表头、切换表头的特效

    做了好几天的固定表头特效,总算是搞定了.先说明一下基本功能:我们在一个网页上浏览很多份表格数据的时候,肯定会碰到很多分不清表头,也分不清表 格是哪个的情况,这个时候我们就希望能有一种功能,就是我们再下 ...

  10. 零成本实现WEB性能测试(二)JMeter基础知识

    特点: 支持多种服务类型进行测试,包括: Web-Http,HTTPS SOAP Database via JDBC LDAP JMS Mail-POP3 & IMAP 支持录制回放方式获取脚 ...