一:lowbit()计算二进制中1的个数

  1. #include <iostream>
  2. using namespace std;
  3. const int N = 100010;
  4. int n;
  5. int a[N];
  6. int lowbit(int x) {
  7. return x & -x; // 这个题目的关键,实现返回某个数二进制的最后一个1。例如9---1001,返回就是1 8 ---- 1000返回就是8
  8. }
  9. int main() {
  10. cin >> n;
  11. while (n--) {
  12. int x;
  13. scanf("%d", &x);
  14. int res = 0;
  15. while (x) {
  16. x -= lowbit(x), res++; // 这里考察了运算符的优先级,这种写法也需要学习。
  17. }
  18. cout << res << " ";
  19. }
  20. return 0;
  21. }

二:位运算之异或运算

image.pngimage.png

  1. class Solution {
  2. public:
  3. int singleNumber(vector<int>& nums) {
  4. int res = 0;
  5. for (auto& c : nums)
  6. {
  7. res ^= c;
  8. }
  9. return res;
  10. }
  11. };

三:区间和

没有找到相应的题目,只能在clion上实验,如果明天面试过了,能拿工资就买一个正版。
image.png