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

首頁 > 編程 > Python > 正文

342. Power of Four【easy】【python】

2019-11-06 08:05:10
字體:
來源:轉載
供稿:網友

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?

Credits:Special thanks to @yukuairoy for adding this PRoblem and creating all test cases.

Subscribe to see which companies asked this question.

題目要求判斷是不是4的冪。可以利用判斷是否是2的冪的方法。

如果一個整數num是2的冪,則num&(num-1)==0,2的冪自然是4的冪,只需要再添加一個條件,即:二進制的num中0的個數是偶數個即可。

代碼:

return num&(num-1)==0 and len(str(bin(num)[3:]))%2==0


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尼勒克县| 河池市| 南川市| 宁明县| 富裕县| 山东| 焦作市| 德庆县| 和顺县| 赤峰市| 封开县| 巴东县| 定兴县| 沙洋县| 精河县| 鄂尔多斯市| 汪清县| 湘乡市| 溆浦县| 都匀市| 永川市| 甘洛县| 平凉市| 景宁| 东辽县| 讷河市| 拜城县| 蒙山县| 武安市| 博罗县| 荃湾区| 新巴尔虎左旗| 武清区| 龙南县| 峡江县| 高清| 湖州市| 兰州市| 永福县| 武山县| 安丘市|