数据结构与算法-位运算

Posted by 小拳头 on Sunday, January 17, 2021

面试题05.07. 配对交换

提取奇数位左移1位, 或上偶数位右移1位.

class Solution {
    public int exchangeBits(int num) {
        return (((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >> 1));
    }
}

参考

  1. labuladong算法
  2. LeetCode刷题活动第二期Week2——位运算专题讲解
  3. leetcode
  4. acwing

comments powered by Disqus