1009. Complement of Base 10 Integer*

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:

  • 0 <= N < 10^9

C++ 实现 1

从 868. Binary Gap* 学习用右移以及按位与 & 来获取二进制的每一位 binary. 为了获取合适长度的位而不是一直执行循环 32 次, 用 sum 来进行控制. 注意 N == 0 时需要专门处理.

class Solution {public:int bitwiseComplement(int N) {if (N == 0) return 1;int res = 0, sum = 0;for (int i = 0; i < 32; ++ i) {int binary = (N >> i) & 1;sum += (binary << i);if (sum >= N) break;int complement = 1 - binary;res += (complement << i);}return res;}
};

C++ 实现 2

来自 LeetCode Submission.

class Solution {public:int bitwiseComplement(int N) {if (!N)return 1;int exponent = 0;int res = 0;while (N) {// 这里只考虑二进制为 0, 翻转后为 1 的情况if (!(N & 1))res += (1 << exponent);exponent++;N >>= 1;}        return res;}
};

1009. Complement of Base 10 Integer*相关推荐

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

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  2. ValueError: invalid literal for int() with base 10: “ ”

    出现这个错误 :ValueError: invalid literal for int() with base 10: ''  " 或者: ValueError: invalid liter ...

  3. ValueError: invalid literal for int() with base 10

    在运行<机器学习实战>第二章中的代码样例时, 我遇到如下错误: 下面是网上参考文献[1]中的例子 Traceback (most recent call last):   File &qu ...

  4. {ValueError}invalid literal for int() with base 10: ‘1.0‘

    {ValueError}invalid literal for int() with base 10: '1.0' 原因是数字字符串不能直接转int类型,需要转为float类型后才能转int类型: 解 ...

  5. Python 空字符串转化问题:ValueError: invalid literal for int() with base 10: ' ',原因及解决方法。

    ValueError: invalid literal for int() with base 10: ' ' 翻译: 值异常:以10为基数的int()的无效文字:' ' int('')就会报错. 就 ...

  6. ValueError: invalid literal for int() with base 10 与数据类型有关的转换报错

    1.ValueError: invalid literal for int() with base 10 a是一个字符串,例如'108.8' 报错原因:直接对a进行int(a)操作,语法不允许 解决办 ...

  7. ValueError: invalid literal for int() with base 10:Python报错及其解决办法

    https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注! 欢迎关注微信公众号:宝藏女孩的成长日记 如有转载,请注明出处(如不注明,盗者必究) 报错情况 Valu ...

  8. Python中int(input(请输入一个数))报错:ValueError: invalid literal for int() with base 10: '2.7'

    编写下面这段代码: def division():apple = int(input("请输入苹果个数:\n"))children = int(input("请输入孩子个 ...

  9. Python debug —— invalid literal for int() with base 10

    异常出现的直接原因即是,对于一个浮点数的字符('1.4'),直接使用 int 进行强制类型转换: >>> int('1.5') ValueError: invalid literal ...

  10. python invalid literal for int_Python方法int()报错:invalid literal for int() with base 10

    int()函数可以将字符串转换为整型,但是切记int()只能转化由纯数字组成的字符串, 非纯数字组成的字符串强转为整型会报错:ValueError: invalid literal for int() ...

最新文章

  1. 【力扣网练习题】移除元素
  2. java中memcached
  3. ASM-Net:可解释的美学评分及图像剪裁
  4. substr()函数——mysql:截取字符串子串
  5. 最后 24 小时,赶紧来领取这 50 本送书福利吧!
  6. vertx.FileResolver文件解析
  7. 全局模式下的正则表达式
  8. Windows server 2016 安装补丁报错 - The update is not applicable to your computer Error:0x800f0823
  9. bootstrapt使用
  10. V4C3-MXene 二维V4C3Tx迈科烯(MXene),胶体溶剂分散液 ,风琴状多层材料 ,冻干粉末
  11. 晏殊几何学讲义(思维导图)
  12. 透明遮罩图层VS高斯模糊滤镜 效果分析
  13. iOS:注册App ID
  14. 反掩码、掩码和通配符的区别
  15. 计算机课真多,这节电脑课真爽啊
  16. 手把手教你pfx证书转pem
  17. 电子邮件及PE工作盘
  18. Java swing 单机版五子棋
  19. 从零开始学单片机c语言 pdf,从零开始学习单片机.pdf
  20. 计算机在心理学实验中的应用举例,普通高等教育国家级规划教材 实验心理学...

热门文章

  1. 一种基于linux系统的精准流量统计方法
  2. js中的shift()函数
  3. SQL sever 中yyyyMMddmmss字符串转日期
  4. Elasticsearch分布式搜索引擎-安装到实战
  5. 西电计算机学院嵌入式所,祝贺计算机科学与技术学院张亮老师团队论文被顶级期刊TNNLS录取...
  6. 阿里巴巴初创时的十八罗汉,离开阿里之后都有什么故事?
  7. 【计算几何】求三角形外接圆的周长、面积公式
  8. 在 React 中使用 TypeScript、使用CRA创建TS项目、React 中的常用类型
  9. SQLServer简繁互换
  10. 上海双非改考408,与上海计算所联合培养!上海第二工业大学计算机专硕!