LeetCode Number Complement
原题链接在这里:https://leetcode.com/problems/number-complement/
题目:
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Note:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- You could assume no leading zero bit in the integer’s binary representation.
Example 1:
Input: 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
题解:
找到num最左面的1 bit, 从这个位置到最后是要flip的部分. template是从最左位置到最后到时1 bit.
flip原来整体的num, 然后位运算 & template.
Time Complexity: O(1). Space: O(1).
AC Java:
1 public class Solution { 2 public int findComplement(int num) { 3 int template = (Integer.highestOneBit(num) << 1) - 1; 4 num = ~num; 5 return num & template; 6 } 7 }
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6272363.html
LeetCode Number Complement相关推荐
- LeetCode Number of Islands(flood fill)
问题:给出一个由0和1组成的二维网格图(m*n),1表示陆地,0表示水.要求统计有多少块陆地 思路:常见的flood fill算法有三种,深度优先搜索.广度优先搜索以及广度扫描法.广度扫描法其实原理与 ...
- 263.Ugly Number||202 happy number||476 Number Complement||136 Single Number
263.Ugly Number 判断因数是否只有素数2.3.5.. 感觉比较简单: class Solution(object):def isUgly(self, num):""& ...
- 【LeetCode】476. 数字的补数 Number Complement
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...
- leetcode 476. Number Complement | 476. 数字的补数(位运算)
题目 https://leetcode.com/problems/number-complement/ 题解 class Solution {public int findComplement(int ...
- LeetCode之Number Complement
1.题目 Given a positive integer, output its complement number. The complement strategy is to flip the ...
- LeetCode 476. Number Complement
题目: Given a positive integer, output its complement number. The complement strategy is to flip the b ...
- Leetcode 476. Number Complement 补数 解题报告
1 解题思想 这道题给了一个正的整数,然后需要计算一个特殊的补数,这个补数的计算方式是: 1.对于这个整数num,转换成对应的二进制表示,这个二进制表示共有x位(不高于32) 2.将这x位取反后,得到 ...
- 【leetcode】【套路题】476. Number Complement
一.题目描述 Given a positive integer, output its complement number. The complement strategy is to flip th ...
- LeetCode算法题-Number Complement(Java实现-五种解法)
这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...
最新文章
- C#中的几种加密算法整理
- 【vuejs路由】vuejs 路由基础入门实战操作详细指南
- 老板:kill -9的原理都不知道就敢到线上执行,明天不用来了!
- 手把手教你玩转ARP包(四)
- 砥志研思SVM(三) 最优间隔分类器问题(下):软间隔SVM
- Web Service/WCF 部署注意事项。
- 防护针对SQL Server数据库的SQL注入攻击
- Hibernate框架介绍以及入门 【一】Hibernate 快速入门 框架的概述 什么是 Hibernate Hibernate 持久层的ORM框架 下载Hibernate 官网 编写测试代码
- mm1排队论编程c语言,完整版MM1排队系统仿真matlab实验报告
- dos下的for命令详解(zz)
- Ubuntu18.04解决sudo apt-get 出现--无法解析域名“cn.archive.ubuntu.com”的问题
- T.38 和传真的未来
- 《编写高质量代码(改善Java程序的151个建议)》读书笔记
- [循证理论与实践] Meta分析系列之五:贝叶斯 Meta 分析与 WinBUGS 软件
- 从零开始用人工智能预测股票(三、初步实现)
- Python之urlparse模块
- 每日新闻:阿里、商汤设立人工智能实验室;美光科技收购英特尔股份;2019十大战略科技发展趋势;北京明年允许自动驾驶车辆开展测试...
- 知识图谱——知识抽取(1)
- 机器学习LR模型,在面试中经常被问到的问题
- excel打不开是什么原因_网页打不开是什么原因