国产探花免费观看_亚洲丰满少妇自慰呻吟_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后用^運算符補碼.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清远市| 黎平县| 体育| 姚安县| 临潭县| 稻城县| 南丰县| 威宁| 驻马店市| 锡林郭勒盟| 怀安县| 霞浦县| 建德市| 铜陵市| 婺源县| 富裕县| 察哈| 延长县| 江山市| 天水市| 高密市| 澜沧| 邯郸市| 河南省| 从江县| 千阳县| 如东县| 德庆县| 商南县| 简阳市| 甘肃省| 湘乡市| 北京市| 治多县| 紫云| 襄垣县| 淮北市| 兴安盟| 定远县| 彭泽县| 云和县|