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. ...
随机推荐
- [Excel]鼠标右键菜单没有新建Word、Excel、PPT怎么办?
很多朋友在安装好Office(2010或2013等)之后,发现右键新建中没有Word.Excel.PowerPoint等项,但是自己的Office却明明安装好了.这个时候该怎么办呢?这里,本文为大家提 ...
- Greeplum 系列(四) 数据的装载与卸裁
Greeplum 系列(四) 数据的装载与卸裁 装载数据有以下种方法: insert copy 外部表 gpload 下面以 member_delta 表为例分别介绍这四种方法. create tab ...
- 去除json数据的某些键值对
假如现在要处理的原始数据是字符串.形式如下: var vJson = { name: "张三", class: "软件工程一班" ,other:"无效 ...
- win10 startup启动目录路径命令
仅对当前用户生效: C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 开始-运行 shel ...
- chrome crx下载路径
chrome crx下载后会被删除,可在检查时粘贴出来,下载路径在: %localappdata%\Google\Chrome\User Data\Webstore Downloads 参考:http ...
- crontab定期执行shell脚本
[场景]: 需要定期运行Hadoop的MapReduce [解决办法]: 编写一个运行MapReduce的Shell脚本.然后使用crontab配置定时任务. [Shell脚本] #!/usr/bin ...
- LWIP内存管理
LWIP是一种TCP/IP协议栈,与嵌入式操作系统一样也提供了内存管理. 内存池里面有多个同样大小的内存,不同类型的内存池其里面的内存大小不一样.
- CentOS下的Git服务器
[Gitosis]CentOS下的Git服务器:Gitosis [摘要] 详细介绍如何在CentOS上配置Gitosis 我们很多人知道Git可能是从Github开始的 ...
- 说说javap命令
javap定义 javap是 Java class文件分解器,可以反编译(即对javac编译的文件进行反编译),也可以查看java编译器生成的字节码.用于分解class文件. 测试类 public c ...
- SPOJ - AMR11A(DP)
Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did ...