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.

代码:

class Solution {
public:
int bitwiseComplement(int N) {
if(N == 0) return 1;
vector<int> ans;
while(N) {
ans.push_back(!(N % 2));
N /= 2;
}
int sum = 0;
for(int i = 0; i < ans.size() / 2; i ++)
swap(ans[i], ans[ans.size() - i - 1]);
for(int i = 0; i < ans.size(); i ++)
sum += ans[i] * pow(2, ans.size() - 1 - i); return sum;
}
int pow(int a, int b) {
int ans = 1;
while(b) {
if(b % 2) {
ans *= a;
b --;
} else {
a *= a;
b /= 2;
}
}
return ans;
}
};

  周末开始啦 

#Leetcode# 1009. Complement of Base 10 Integer的更多相关文章

  1. LeetCode 1012 Complement of Base 10 Integer 解题报告

    题目要求 Every non-negative integer N has a binary representation.  For example, 5 can be represented as ...

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

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

  3. 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 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 ...

  5. 【leetcode】1012. Complement of Base 10 Integer

    题目如下: Every non-negative integer N has a binary representation.  For example, 5 can be represented a ...

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

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

  7. LeetCode——Number Complement

    LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...

  8. convert from base 10 to base 2

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Hence, we convert fro ...

  9. Python中ValueError: invalid literal for int() with base 10 的实用解决办法

    爬虫代理IP由芝麻HTTP服务供应商提供今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了 totalCount = ' totalPage = int(totalCount)/20 Value ...

随机推荐

  1. 某游戏公司(凯英网络)PHP开发工程师笔试题

  2. SAP 维护视图创建与修改

    维护视图创建与修改 维护视图创建 T-CODE:SE54 维护ABAP数据字典 维护已生产的对象 注意:当维护视图修改后,需要删除已生成的对象,重新创建已生成的对象,否则无法显示,这个小窍门我花了半天 ...

  3. XSS 漏洞原理及防御方法

    XSS跨站脚本攻击:两种情况.一种通过外部输入然后直接在浏览器端触发,即反射型XSS:还有一种则是先把利用代码保存在数据库或文件中,当web程序读取利用代码并输出在页面上时触发漏洞,即存储型XSS.D ...

  4. nginx入门与实战

    网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务 就是 Web 网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web 网络服务是一种被动访问 ...

  5. docker学习笔记(一)-vagrant/docker machine安装docker,阿里云通过docker machine安装docker

    首先需要先安装virtualbox https://www.vagrantup.com/ 下载安装vagrant 安装完毕后通过vagrant下载镜像,生成实例 mkdir test_centos7 ...

  6. 5.02-requests_proxy

    import requests # 1.请求url url = 'http://www.baidu.com' headers = { 'User-Agent': 'Mozilla/5.0 (Macin ...

  7. SQlite源码分析--源网站

    http://huili.github.io/B-treeImplementation/hierarchicalorganization.html SQLite中的B-tree SQLite中每个数据 ...

  8. JDK动态代理给Spring事务埋下的坑!

    一.场景分析 最近做项目遇到了一个很奇怪的问题,大致的业务场景是这样的:我们首先设定两个事务,事务parent和事务child,在Controller里边同时调用这两个方法,示例代码如下: 1.场景A ...

  9. 如何在关闭ssh连接的情况下,让进程继续运行?

    #screen 1 回车后进入Screen子界面,此时putty标题栏会指示处于子界面状态,然后运行你的程序 #应用程序名 1 然后按下Ctrl+A后抬起,然后按下d键,此时切换回主界面,Putty的 ...

  10. python的格式化输出

    Python的格式化输出有两种: 一.类似于C语言的printf的方法 二.类似于C#的方法