#Leetcode# 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.
代码:
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的更多相关文章
- LeetCode 1012 Complement of Base 10 Integer 解题报告
题目要求 Every non-negative integer N has a binary representation. For example, 5 can be represented as ...
- LeetCode.1009-十进制数的补码(Complement of Base 10 Integer)
这是小川的第377次更新,第404篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第238题(顺位题号是1009).每个非负整数N都具有二进制表示.例如,5可以二进制表示为 ...
- 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- [Swift]LeetCode1009. 十进制整数的补码 | Complement of Base 10 Integer
Every non-negative integer N has a binary representation. For example, 5 can be represented as &quo ...
- LeetCode——Number Complement
LeetCode--Number Complement Question Given a positive integer, output its complement number. The com ...
- convert from base 10 to base 2
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Hence, we convert fro ...
- Python中ValueError: invalid literal for int() with base 10 的实用解决办法
爬虫代理IP由芝麻HTTP服务供应商提供今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了 totalCount = ' totalPage = int(totalCount)/20 Value ...
随机推荐
- Python: 内置私有方法
################## __new__ ##################@staticmethod__new__(cls [, ...])类的构造器,创建某个类的实例,返回值应该是c ...
- Java String、StringBuilder、StringBuffer[笔记]
String对象是字符串常量(创建之后不可更改),StringBuilder和StringBuffer对象是字符串变量(可更改),三者主要区别在于执行速度和线程安全. 执行速度 执行速度:String ...
- 强大而灵活的字体图标替代库iconfont
前言概述 在开发网页制作过程中通常需要一些网页小图标,前端需要PS切图,将单个小图标icon组合成CSS雪碧图过程中,需要一些时间和精力; 如果网页制作中需要的小图标icon有一套css框架库集 ...
- python 浅谈小数据池和编码
⼀. ⼩数据池 在说⼩数据池之前. 我们先看⼀个概念. 什么是代码块: 根据提示我们从官⽅⽂档找到了这样的说法: A Python program is constructed from code b ...
- Java面试知识点之数据库篇(一)
前言:数据库的相关知识,在面试中也经常出现,笔者认为非常有必要对此类知识进行相关总结. 1.索引 索引是对数据库表中一列或多列的值进行排序的结构,是帮助数据库高效获取数据的数据结构. 通俗理解:索引就 ...
- pip freeze 打包依赖库及setup.py
需要打包的工程目录下使用命令: pip freeze > requirements.txt 就会在pip目录生成 requirements.txt 文件,该文件内就是当前环境所安装的所有扩展包打 ...
- Python单元测试框架 unittest详解
一 整体结构概览 unittest原名为PyUnit,是由java的JUnit衍生而来.对于单元测试,需要设置预先条件,对比预期结果和实际结果. TestCase :通过继承TestCase类,我们可 ...
- Redis主从数据库同步
Redis主从同步原理-SYNC和MySQL主从复制的原因一样,Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采 ...
- 吴恩达课后作业学习2-week1-1 初始化
参考:https://blog.csdn.net/u013733326/article/details/79847918 希望大家直接到上面的网址去查看代码,下面是本人的笔记 初始化.正则化.梯度校验 ...
- JSONPath介绍
1. JSONPath介绍 官网地址: https://github.com/alibaba/fastjson/wiki/JSONPath fastjson 1.2.0之后的版本支持JSONPath. ...