Python 基础知识简单

八进制到二进制转换

“八进制到二进制转换”问题的详细指南和 Python 实现。

问题陈述

简单

编写一个函数 octal_to_binary(octal_str) ,它接受一个表示八进制数(以 8 为基数,数字 0-7)的字符串,并返回一个表示其等效二进制(以 2 为基数)的字符串。不要在输出中包含前导零(输入“0”除外)。

提示:将每个八进制数字转换为其 3 位二进制数字并连接。

约束条件
  • 1 <= len(octal_str) <= 10
  • octal_str contains only digits '0' through '7'

示例

Example 1
Input
octal_to_binary('12')
Output
'1010'
Explanation

1 = 001, 2 = 010. Concatenate: 001010. Remove leading zeros: 1010.

Example 2
Input
octal_to_binary('77')
Output
'111111'
Explanation

7 = 111, 7 = 111. Concatenate: 111111.

Example 3
Input
octal_to_binary('312')
Output
'11001010'
Explanation

3 = 011, 1 = 001, 2 = 010. Concatenate: 011001010. Remove leading zero: 11001010.

Need a Hint?
考虑使用特定于数字的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。