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 "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
题意啥的去看看中文版的就好了。简单一点看看example
大佬的代码很短的
class Solution {
public:
int bitwiseComplement(int N) {
int num[] = {};
int cnt = ;
int sum = ;
int flag = ;
if(N == ){
return ;
}
while(N){
int pos = N % ;
num[cnt++] = pos == ? : ;
N/=;
}
for(int i = ; i < cnt ; i++){
//cout<<num[i]<<endl;
if(num[i] == ){
sum += ;
}else{
sum += flag;
}
flag *= ;
}
//cout<<endl;
return sum;
}
};
128th LeetCode Weekly Contest Complement of Base 10 Integer的更多相关文章
- 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- #Leetcode# 1009. Complement of Base 10 Integer
https://leetcode.com/problems/complement-of-base-10-integer/ Every non-negative integer N has a bina ...
- LeetCode.1009-十进制数的补码(Complement of Base 10 Integer)
这是小川的第377次更新,第404篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第238题(顺位题号是1009).每个非负整数N都具有二进制表示.例如,5可以二进制表示为 ...
- LeetCode 1012 Complement of Base 10 Integer 解题报告
题目要求 Every non-negative integer N has a binary representation. For example, 5 can be represented as ...
- 128th LeetCode Weekly Contest Capacity To Ship Packages Within D Days
A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...
- 128th LeetCode Weekly Contest Pairs of Songs With Total Durations Divisible by 60
In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...
- [Swift]LeetCode1009. 十进制整数的补码 | Complement of Base 10 Integer
Every non-negative integer N has a binary representation. For example, 5 can be represented as &quo ...
- 123th LeetCode Weekly Contest Add to Array-Form of Integer
For a non-negative integer X, the array-form of X is an array of its digits in left to right order. ...
随机推荐
- ssh上外网
https://www.cnblogs.com/leipei2352/archive/2011/07/21/2112274.html http://www.qijiannet.com/web/1332 ...
- Fix “Could Not Find This Item” When Deleting in Windows 7
If you’ve been using Windows for as long as I have, you have probably run into your share of weird e ...
- Vmware下CentOs7 桥接模式下配置固定IP
1.安装完CentOS7后 修改虚拟机网络适配器配置:改成桥接模式 2.设置Vmware的 编辑->虚拟网络编辑器 3.重启CentOs7 , 查看网络 # ip addr interface是 ...
- eclipse查看jar包中的class文件出现乱码
参考:http://blog.csdn.net/bell2008/article/details/40978959 1,问题来源是在eclipse中直接查看springside的class(由ecli ...
- C# GDI+绘图介绍
最近查阅网上资料,将GDI+的基本知识汇总如下: 一.基本的知识 GDI+:Graphics Device Interface Plus也就是图形设备接口,提供了各种丰富的图形图像处理功能; 在C#. ...
- 基于NIOS II的双端口CAN通信回环测试
基于NIOS II的双端口CAN通信回环测试 小梅哥编写,未经授权,严禁用于任何商业用途 说明:本稿件为初稿,如果大家在使用的过程中有什么疑问或者补充,或者需要本文中所述工程源文件,欢迎以邮件形式发送 ...
- 一类适合初学者的DP:最大子段和与最大子矩阵
最近在水简单DP题,遇到了两道层层递进的DP题,于是记录一下 一.最大子段和 题意: 给出一个长度为n(n<=1e5)的序列,求连续子段的最大值 比如说2 3 -4 5 的最大值是6 而 2 ...
- 大数据hadoop分布式系统
1>hadoop简介: Hadoop是一个开发和运行处理大规模数据的软件平台,是Apache的一个用java语言实现开源软件框架,实现在大量计算机组成的集群中对海量数据进行 分布式计算 ...
- vmware之VMware Remote Console (VMRC) SDK(二)
在上篇文章中,我们用winform结合vmware的api做了一个demo,在vmrc sdk中作为plugin单独打包出来vmware-vmrc-win32-x86.exe,上篇文章的demo只能基 ...
- [Mac][转] ports命令
[Mac][转] ports命令 安装路径:/opt/local/lib/ 常用命令 port -d selfupdate #升级macport, 如同:cd /usr/ports && ...