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

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

Leetcode-Algorithms Number Complement(數字補碼)

2019-11-08 02:08:08
字體:
來源:轉載
供稿:網友

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.

如題數字補碼就是求本身的補碼后return其十進制的數。 二進制5 = 101(無視正負位的數->leading bit) 其補碼 010 = 2

class Solution(object): def findComplement(self, num): """ :type num: int :rtype: int """ return (1<<len("{0:b}".format(num)))-1 ^ num

首先將補位到和二進制帶leading bits的num相同長度(10000…..), -1后用^運算符補碼.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉首市| 马龙县| 建始县| 蛟河市| 乐安县| 双柏县| 沂水县| 邻水| 阜南县| 班玛县| 应城市| 吐鲁番市| 上犹县| 双峰县| 厦门市| 孝昌县| 安图县| 甘孜| 华池县| 墨脱县| 康马县| 怀集县| 凉城县| 萨迦县| 嫩江县| 册亨县| 鸡西市| 德格县| 渭南市| 临沧市| 收藏| 张北县| 康平县| 泰和县| 黎城县| 滦南县| 专栏| 海城市| 溧阳市| 漳平市| 贺州市|