476. Number Complement

本文介绍了一种简单的方法来实现数字的二进制补数转换。通过按位取反操作,可以快速得到输入整数的补数表示,并提供了一个C++示例代码。

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

例子:5(101)-> 2(010)    1(1)-> 0(0)

题目的要求很清楚,就是将数字的二进制按位取反,然后返回新的数。代码很简单,这里不再赘述:
class Solution {
public:
    int findComplement(int num) {
        int result = 0;
        int exp = 0;
        while (num) {
            int temp = !(num & 1);
            result += (pow(2, exp) * temp);
            exp += 1;
            num >>= 1;
        }
        return result;
    }
};


因为字数限制 下面目录只是部分 day10-Redis面试篇│ Redis面试篇-01.Redis主从-搭建主从集群.mp4│ Redis面试篇-02.Redis主从-主从同步原理.mp4│ Redis面试篇-03.Redis主从-主从集群优化.mp4│ Redis面试篇-04.Reids主从-哨兵原理.mp4│ Redis面试篇-05.Redis主从-搭建哨兵集群.mp4│ Redis面试篇-06.Redis分片集群-搭建分片集群.mp4│ Redis面试篇-07.Redis分片集群-散列插槽.mp4│ Redis面试篇-08.Redis数据结构-RedisObject.mp4│ Redis面试篇-09.Redis数据结构-SkipList.mp4│ Redis面试篇-10.Redis数据结构-SortedSet.mp4│ Redis面试篇-11.Redis内存回收-过期KEY处理.mp4│ Redis面试篇-12.Redis内存回收-内存淘汰策略.mp4│ Redis面试篇-13.Redis缓存-缓存一致性.mp4│ Redis面试篇-14.Redis缓存-缓存穿透.mp4│ Redis面试篇-15.Redis缓存-缓存雪崩.mp4│ Redis面试篇-16.Redis缓存-缓存击穿.mp4│ ├─day11-微服务面试篇│ 微服务面试篇-01.分布式事务-CAP和BASE.mp4│ 微服务面试篇-02.分布式事务-AT模式脏写问题.mp4│ 微服务面试篇-03.分布式事务-TCC模式.mp4│ 微服务面试篇-04.分布式事务-最大努力通知.mp4│ 微服务面试篇-05.注册中心-环境隔离.mp4│ 微服务面试篇-06.注册中心-服务分级模型.mp4│ 微服务面试篇-07.注册中心-Eureka和Nacos.mp4│ 微服务面试篇-08.远程调用-源码跟踪及负载均衡原理.mp4│ 微服务面试篇-09.远程调用-切换负载均衡算法.mp4│ 微服务面试篇-10.服务保护-线程隔离方案对比.mp4│ 微服务面试篇-11.服务保护-滑动窗口计数器算法.mp4│ 微服务面试篇-12.服务保护-漏桶算法.mp4│ 微服务面试篇-13.服务保护-令牌桶算法.mp4│ 完结撒花.mp4│└─资料.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值