leetcode------Reverse Bits

本文介绍了如何使用Python解决32位无符号整数翻转问题,并探讨了针对频繁调用场景的优化策略。通过实例代码展示了翻转整数的过程,并提供了后续调用优化的思考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

标题:Reverse Bits
通过率:27.6%
难度:简单

Reverse bits of a given 32 bits unsigned integer.

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

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

Related problem: Reverse Integer

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这个题还是思路还是比较明确的。需要注意的就是补位的问题,给的数据不一定都是32位,所以要补位,本题我用的python,

直接代码:

 1 class Solution:
 2     # @param n, an integer
 3     # @return an integer
 4     def reverseBits(self, n):
 5         tmp=bin(n)
 6         tmp=tmp[2:]
 7         if len(tmp)!=32:
 8             for a in range(32-len(tmp)):
 9                 tmp='0'+tmp
10         tmp=tmp[::-1]
11         return int(tmp,2)

 

转载于:https://www.cnblogs.com/pkuYang/p/4322152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值