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

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

Leetcode 190. Reverse Bits

2019-11-10 22:59:59
字體:
來源:轉載
供稿:網友

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (rePResented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

Follow up: If this function is called many times, how would you optimize it?

s思路: 1. 一種方法是右移得到數位,然后左移構造新數。麻煩的地方在于是unsigned integer,貌似不影響。 2. 另一種方法是兩頭取bit位,swap。這樣就和unsigned integer無關! 3. 如果調用多次,如何優化?

//方法1:移位。class Solution {public: uint32_t reverseBits(uint32_t n) { // uint32_t res=0; for(int i=0;i<32;i++){ res<<=1; if(n){ res=res|n&1; n>>=1; } } return res; }};//方法2:swap首尾class Solution {public: uint32_t reverseBits(uint32_t n) { // int i=0,j=31; while(i<j){ int l=n&(1<<i); int r=n&(1<<j); if(l&&!r||!l&&r){ n=n^(1<<j); n=n^(1<<i); } i++;j--; } return n; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新平| 兴和县| 饶阳县| 宁陵县| 洛阳市| 永清县| 济源市| 晋州市| 承德市| 大城县| 荔浦县| 稻城县| 连州市| 湖南省| 双鸭山市| 佳木斯市| 中方县| 东乡族自治县| 微博| 肥城市| 清涧县| 花莲县| 绥化市| 铜鼓县| 盘锦市| 吉水县| 鄄城县| 钟祥市| 永新县| 康平县| 策勒县| 巴楚县| 郑州市| 武山县| 华阴市| 霸州市| 永仁县| 柏乡县| 桃园县| 普陀区| 景谷|