国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LeetCode題解: Number Complement

2019-11-14 10:37:56
字體:
來源:轉載
供稿:網友

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: 5Output: 2Explanation: 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: 1Output: 0Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

思路:

數清leading zeros,翻轉位,然后清掉不需要的位。

題解:

int findComplement(int num) {    int zeros(0);    for(; zeros < 32; ++zeros) {        if (num & (1 << 31)) {            break;        }        num <<= 1;    }    num = ~num;    for(; zeros > 0; --zeros) {        num >>= 1;    }    return num;}


上一篇:[BZOJ3282][LCT]Tree

下一篇:sql索引語法

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 古田县| 黄山市| 龙山县| 莒南县| 兰溪市| 江城| 彰化县| 平乡县| 盐城市| 巢湖市| 高淳县| 浙江省| 同江市| 赣榆县| 绵阳市| 甘孜县| 东安县| 靖宇县| 河北省| 德清县| 兴宁市| 友谊县| 瓦房店市| 乡城县| 双柏县| 西青区| 五常市| 原阳县| 军事| 平遥县| 松原市| 阿合奇县| 庆安县| 故城县| 阿瓦提县| 砚山县| 永嘉县| 垦利县| 广宗县| 临澧县| 东莞市|