【leetcode】1012. Complement of Base 10 Integer
题目如下:
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 forN = 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 a0
and0
to a1
. 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:
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的更多相关文章
- 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 1012 Complement of Base 10 Integer 解题报告
题目要求 Every non-negative integer N has a binary representation. For example, 5 can be represented as ...
- 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 ...
- LeetCode.1009-十进制数的补码(Complement of Base 10 Integer)
这是小川的第377次更新,第404篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第238题(顺位题号是1009).每个非负整数N都具有二进制表示.例如,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 ...
- 【leetcode】1017. Convert to Base -2
题目如下: Given a number N, return a string consisting of "0"s and "1"s that represe ...
- 【LeetCode】将罗马数字转换成10进制数
Roman to Integer Given a roman numeral, convert it to an integer. 首先介绍罗马数字 罗马数字共有七个,即I(1),V(5),X(10) ...
- 【leetcode】1012. Numbers With Repeated Digits
题目如下: Given a positive integer N, return the number of positive integers less than or equal to N tha ...
- [Swift]LeetCode1009. 十进制整数的补码 | Complement of Base 10 Integer
Every non-negative integer N has a binary representation. For example, 5 can be represented as &quo ...
随机推荐
- 高级Javascript代码
Javascript是一门很吊的语言,我可能学了假的JavaScript,哈哈,大家还有什么推荐的,补充送那啥邀请码. 本文秉承着:你看不懂是你SB,我写的代码就要牛逼. 1.单行写一个评级组件 &q ...
- 【CDN+】 Hbase入门 以及Hbase shell基础命令
前言 大数据的基础离不开Hbase, 本文就hbase的基础概念,特点,以及框架进行简介, 实际操作种需要注意hbase shell的使用. Hbase 基础 官网:https://hbase.ap ...
- spring4.1.8扩展实战之五:改变bean的定义(BeanFactoryPostProcessor接口)
本章我们继续实战spring的扩展能力,通过自定义BeanFactoryPostProcessor接口的实现类,来对bean实例做一些控制: 原文地址:https://blog.csdn.net/bo ...
- HTTP 协议解析
目录 目录 HTTP 协议 HTTP 协议工作原理 HTTP Request 请求行 Request Header HTTP Response 状态行 Response Header Body HTT ...
- 拒绝从入门到放弃_《Python 核心编程 (第二版)》必读目录
目录 目录 关于这本书 必看知识点 最后 关于这本书 <Python 核心编程 (第二版)>是一本 Python 编程的入门书,分为 Python 核心(其实并不核心,应该叫基础) 和 高 ...
- 利用Fiddler对手机应用抓包
1.启动Fiddler,打开菜单栏中的 Tools > Fiddler Options,打开“Fiddler Options”对话框. 2.在Fiddler Options”对话框切换到“Con ...
- Spring MVC浅析
讲到MVC,想必大家都很熟悉,就是将数据模型.视图.控制器进行分离,做到分工明确,在Spring的帮助下,Spring MVC 更是做到了充分的解耦,因为大部分的资源都由Spring进行管理,为Spr ...
- GD Library extension not available
在后台文章上传封面时,遇到了这样一个错误 GD Library extension not available with this PHP installation Ubuntu Nginx 自己在本 ...
- 阿里巴巴离线数据同步工具/平台datax安装、使用笔记
废话不多说,直接上笔记,先来看下参考链接GitHub: https://github.com/alibaba/DataX.此链接有较详细的安装使用方法,还有json参数编写的文档说明,建议多看. Fi ...
- Linux的mysql部署
1. 先输入代码yum install wget -y才可以做后面的 2.下载并安装MySQL官方的 Yum Repository 代码: wget -i -c http://dev.mysql ...