国产探花免费观看_亚洲丰满少妇自慰呻吟_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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 思茅市| 东丰县| 嵩明县| 泰安市| 马尔康县| 黔东| 山丹县| 黑水县| 城步| 盐津县| 茂名市| 罗江县| 黎川县| 洛南县| 阿坝| 朝阳县| 长子县| 疏附县| 绿春县| 九江市| 正蓝旗| 乌鲁木齐市| 华池县| 肇庆市| 镇康县| 阿克苏市| 铜陵市| 灵寿县| 扎兰屯市| 新丰县| 山阳县| 麻城市| 泽库县| 西宁市| 华池县| 邹城市| 专栏| 桓台县| 高要市| 武冈市| 望城县|