作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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 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

题目大意

求一个数的补码。

解题方法

很简单一个作弊方法,直接求二进制,然后把1改成0,把0改成1即可。

Python代码如下:

class Solution(object):
def bitwiseComplement(self, N):
"""
:type N: int
:rtype: int
"""
return int("".join(map(lambda x :"0" if x == "1" else "1", bin(N)[2:])), 2)

日期

2019 年 3 月 21 日 —— 好久不刷题,重拾有点难

最新文章

  1. 再部署一个 instance 和 Local Network - 每天5分钟玩转 OpenStack(131)
  2. MySql索引总结
  3. CSS笔记2
  4. 基于canvas实现物理运动效果与动画效果(一)
  5. js事件委托,可以使新添加的元素具有事件(event运用)
  6. PLSQL_性能优化系列15_Oracle Explain Plan解析计划解读
  7. 服装销售系统数据库课程设计(MVC)
  8. java 数据库两种连接方法
  9. php单词里的大写字母
  10. 互联网程序设计c++
  11. 一次__libc_message的排查
  12. Linux显示各栏位的标题信息列
  13. iOS 初探代码混淆(OC)
  14. Axure原型设计工具介绍
  15. ADO.NET学习笔记(1)
  16. python学习2:turtle的使用蟒蛇绘制的学习以及自己摸索的等边三角形绘制(跟随mooc学习)
  17. Apache无法正常启动(配置多个监听端口)
  18. js 异步请求
  19. LeetCode--844--比较含退格的字符串(java)
  20. asp .net api 日志

热门文章

  1. dlang 读取gz压缩文件
  2. Excel-同个工作簿中,多个工作表中有格式一致的表格,做汇总透视合并信息
  3. linux下vim的安装与配置(centos)
  4. 修复UE4编辑器,ClearLog操作导致的崩溃
  5. org.apache.hadoop.hive.ql.metadata.HiveException: Internal Error: cannot generate all output rows for a Partition解决
  6. 大数据学习day29-----spark09-------1. 练习: 统计店铺按月份的销售额和累计到该月的总销售额(SQL, DSL,RDD) 2. 分组topN的实现(row_number(), rank(), dense_rank()方法的区别)3. spark自定义函数-UDF
  7. Java——数组的定义与使用
  8. spring boot集成mybatis框架
  9. @NotBlank 注解不生效
  10. 【Spring Framework】Spring入门教程(四)注册Bean到IOC容器