#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 ...
随机推荐
- java求解第N个素数(质数)
面试中,遇到一个题目:求解第N个素数. import java.util.Scanner; public class GetPrimeNumber { public static int NthPri ...
- February 14th, 2018 Week 7th Wednesday
Love does not dominate, it culitvates. 爱不是羁绊,而是成就. Love should not wipe out everything you are, love ...
- Lingo求解线性规划案例1——生产计划问题
凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 说明: Lingo版本: 某工厂明年根据合同,每个季度末 ...
- (1)Python基础
几种常用类型 int float str bool 基本数值操作 绝对值 abs 四舍五入 round 最大值&最小值
- java8 流操作
0 创建流 public void test1(){ List<String> list = new ArrayList<>(); Stream<String> ...
- C++ 星号* 与 引用&
星号 * 1. 声明的时候有*, 表示指针变量 int *p=&a;// '&'的作用就是把a变量在内存中的地址给提取出来 2. * +地址, 表示地址操作符 3. 数字*数字, 表示 ...
- 强化学习(一)—— 基本概念及马尔科夫决策过程(MDP)
1.策略与环境模型 强化学习是继监督学习和无监督学习之后的第三种机器学习方法.强化学习的整个过程如下图所示: 具体的过程可以分解为三个步骤: 1)根据当前的状态 $s_t$ 选择要执行的动作 $ a_ ...
- 003_生成器(generator)内部解析
#http://kb.cnblogs.com/page/87128/(未看完)
- ESP8266烧录配置
装载的网页在工程目录下同个文件夹data
- hdu2121 Ice_cream's world II
hdu2121 Ice_cream's world II 给一个有向图,求最小树形图,并输出根节点 \(n\leq10^3,\ m\leq10^4\) 最小树形图 对于求无根最小树形图,可以建一个虚拟 ...