题目如下:

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. 利用AddressBook.framework框架获取iOS系统通讯录数据

    此方法是使用AddressBook.framework框架获取通讯录信息 第一步.在info.plist文件里面配置访问权限 第二步.导入头文件 #import <AddressBook/Add ...

  2. Python模块学习之xlrd、xlutils、openpyxl 读写/追加Excel文件

    Python操作Excel的四个工具包 xlrd: 对Excel进行读相关操作,注意只能操作 .xls xlwt: 对Excel进行写相关操作,注意只能操作 .xls,且只能创建一个全新的Excel然 ...

  3. spss如何选择需要的变量?

    spss如何选择需要的变量? 今天一位网友问我,spss如何在许多字段(变量)中选择我需要的字段,而不显示其他的字段呢? 这个问题问的很好,在实际的数据分析或者挖掘的过程中,都需要用这个来找出对商业问 ...

  4. JQuery 字符串转时间格式

    //字符串转时间格式 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$) ...

  5. WEB服务端安全---认证会话与访问控制

    一.认证与会话管理 认证:简而言之就是通过一定的凭证认出用户是谁.认证过程中按凭证数量可简单分为 ‘单因素认证’.‘双因素认证’或多因素认证.一般来说,多因素认证强度更高,但是用户体验上会比单因素认证 ...

  6. (appium+python)UI自动化_09_unittest批量运行测试用例&生成测试报告

    前言 上篇文章[(appium+python)UI自动化_08_unittest编写测试用例]讲到如何使用unittets编写测试用例,并执行测试文件.接下来讲解下unittest如何批量执行测试文件 ...

  7. upc组队赛17 Bits Reverse【暴力枚举】

    Bits Reverse 题目链接 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ...

  8. 继承Process类,计算累加和以及阶乘

    #定义一个类 继承Process类 from multiprocessing import Process import os class Download(Process): def __init_ ...

  9. 《JAVA设计模式》之命令模式(Command)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述命令(Command)模式的: 命令模式属于对象的行为模式.命令模式又称为行动(Action)模式或交易(Transaction)模式. ...

  10. [Linux] 011 其它权限管理命令

    1. 权限管理命令:chown 命令名称:chown 命令英文原意:change file ownership 命令所在路径:/bin/chown 执行权限:所有用户 语法:chown [用户] [文 ...