Python 刷Leetcode题库,顺带学英语单词(42)

简介: Python 刷Leetcode题库,顺带学英语单词(42)

Reverse Bits

Reverse bits of a given 32 bits unsigned integer.     [#190]


   Example 1:

   Input: 00000010100101000001111010011100

   Output: 00111001011110000010100101000000

   Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.


   Example 2:

   Input: 11111111111111111111111111111101

   Output: 10111111111111111111111111111111

   Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10101111110010110010011101101001.


Note:

Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.


In Java, the compiler represents the signed integers using 2's complement notation.

Therefore, in Example 2 above the input represents the signed integer -3 and the output represents the signed integer -1073741825 .



Number of 1 Bits

Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).      [#191]

   Example 1:

   Input: 00000000000000000000000000001011

   Output: 3

   Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.


   Example 2:

   Input: 00000000000000000000000010000000

   Output: 1

   Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.


   Example 3:

   Input: 11111111111111111111111111111101

   Output: 31

   Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.


Note:


Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.

In Java, the compiler represents the signed integers using 2's complement notation.

Therefore, in Example 3 above the input represents the signed integer -3 .



目录
相关文章
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
51 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
99 2
|
13天前
|
JSON 数据格式 Python
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
这篇文章介绍了一个Python脚本,用于统计TXT或JSON文件中特定单词的出现次数。它包含两个函数,分别处理文本和JSON文件,并通过命令行参数接收文件路径、目标单词和文件格式。文章还提供了代码逻辑的解释和示例用法。
25 0
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
|
17天前
Leetcode(最后一个单词长度)
这篇文章介绍了两种解决LeetCode第58题的方法,即计算给定字符串中最后一个单词的长度,方法包括翻转字符串和逆向遍历统计。
15 0
|
17天前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
14 0
|
21天前
|
小程序 IDE 开发工具
Python编写单词复习小程序
Python编写单词复习小程序
10 0
|
2月前
|
算法
LeetCode第58题最后一个单词的长度
LeetCode第58题"最后一个单词的长度"的解题方法,通过从字符串末尾向前遍历并计数非空格字符,直接得出最后一个单词的长度。
LeetCode第58题最后一个单词的长度
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
48 7
|
2月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
45 3
|
2月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
21 3