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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode題解: Number Complement

2019-11-14 10:36:41
字體:
供稿:網(wǎng)友

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.

思路:

數(shù)清leading zeros,翻轉(zhuǎn)位,然后清掉不需要的位。

題解:

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;}


上一篇:MD5加密

下一篇:curl模擬post請求提交

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 甘孜县| 铁力市| 桦甸市| 高州市| 东丽区| 苗栗县| 南乐县| 晋中市| 苍梧县| 凤城市| 通化县| 新绛县| 玉环县| 广州市| 贡嘎县| 三原县| 永善县| 广水市| 高青县| 离岛区| 平远县| 陈巴尔虎旗| 泾阳县| 大石桥市| 洛宁县| 财经| 阿拉善盟| 嵩明县| 闽清县| 峨边| 丰原市| 罗甸县| 乃东县| 政和县| 朝阳县| 寻甸| 昌平区| 南江县| 灵武市| 屏南县| 绥中县|