int('1010',2)的时间复杂度是多少?

2024-04-19 03:06:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想知道python中int(<binary string>,2)的时间复杂性,以便将base-2二进制数字符串转换为int


Tags: 字符串basestring时间二进制int复杂性binary
1条回答
网友
1楼 · 发布于 2024-04-19 03:06:12

根据Python source code,从以2为基数或以2为基数的任何幂次转换时,与字符数有关的是O(N)。在

/* *str points to the first digit in a string of base `base` digits.  base
 * is a power of 2 (2, 4, 8, 16, or 32).  *str is set to point to the first
 * non-digit (which may be *str!).  A normalized int is returned.
 * The point to this routine is that it takes time linear in the number of
 * string characters.

相反,似乎每一个非二次幂都是(通常是?)O(N^2)。在

^{pr2}$

相关问题 更多 >