题目如下:

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

解题思路:题目很简单,把Input转成二进制形式,然后对每一位取反,最后再转成十进制就可以了。

代码如下:

class Solution(object):
def bitwiseComplement(self, N):
"""
:type N: int
:rtype: int
"""
SN = bin(N)[2:]
res = ''
for i in SN:
if i == '':
res += ''
continue
res += ''
return int(res,2)

【leetcode】1012. 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 as ...

  3. 128th LeetCode Weekly Contest Complement of Base 10 Integer

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

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

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

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

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

  6. 【leetcode】1017. Convert to Base -2

    题目如下: Given a number N, return a string consisting of "0"s and "1"s that represe ...

  7. 【LeetCode】将罗马数字转换成10进制数

    Roman to Integer Given a roman numeral, convert it to an integer. 首先介绍罗马数字 罗马数字共有七个,即I(1),V(5),X(10) ...

  8. 【leetcode】1012. Numbers With Repeated Digits

    题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...

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

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

随机推荐

  1. vue概念

    Vue是单向数据流还是双向数据绑定? Vue是单向数据流不是双向数据绑定 Vue的双向数据绑定不过是语法糖(语法糖本质就是一种新的编码方式,并没有给语言增加新的功能.语法糖目的就是为了让代码更易读,更 ...

  2. JavaScript fetch接口

    JavaScript fetch接口 如果看网上的fetch教程,会首先对比XMLHttpRequest和fetch的优劣,然后引出一堆看了很快会忘记的内容(本人记性不好).因此,我写一篇关于fetc ...

  3. linux系统忘记root密码的解决办法--(超细致讲解!)

    在本博客中我采用虚拟机和radhet6.5作为示范: 首先:重新系统系统,在系统重新启动是未进入系统检查之前按下"e"键: 一定要在倒计时到0秒之前按下"e", ...

  4. Spring Boot 读取外部的配置文件

    Spring Boot 程序会按优先级从下面这些路径来加载application.properties 或者 application.yml 配置文件 jar包同级目录下的/config目录jar包同 ...

  5. SqlSession 内部运行

    <深入浅出MyBatis技术原理与实战>p150页 SqlSession内部运行图 四大对象在流程中的操作. 1.准备sql.StatementHandler 的prepare方法进行sq ...

  6. 16/7/8_PHP-书写规范 PHP Coding Standard

    变量命名规范这里感觉 打算采用 匈牙利命名法+驼峰法命名,因为 PHP是弱类型语言,很多时间因为忽略了变量类型而导致犯一些低级错误.所以在前面加上类型名有助于更好的理解代码. 下载是转载 PHP书写规 ...

  7. Netty之大动脉Pipeline

    Pipeline 设计原理 Channel 与ChannelPipeline: 相信大家都已经知道,在Netty 中每个Channel 都有且仅有一个ChannelPipeline 与之对应,它们的组 ...

  8. 使用bootstrap制作网站导航

    除了制作选项卡和下拉菜单,bootstrap还能编写出美观的网站导航栏 一.仿知乎导航栏 <body> <nav class="navbar navbar-default ...

  9. NGUI的button的创建的问题(Button Script)

    一,我们可以给了label,sprite等添加button事件 我们先添加一个label在UI_Root上,然后选中该label,右键-Attach-Box Collider,添加,当你添加完了Box ...

  10. react native 之 Android studio

    https\://services.gradle.org/distributions/gradle-4.4-all.zip 下载失败 https://blog.csdn.net/u013132758/ ...