Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

时间复杂度 线性对数阶python代码貌似有问题 #101

Closed
msk397 opened this issue Dec 13, 2022 · 3 comments
Closed

时间复杂度 线性对数阶python代码貌似有问题 #101

msk397 opened this issue Dec 13, 2022 · 3 comments

Comments

@msk397
Copy link
Contributor

msk397 commented Dec 13, 2022

```
""" 线性对数阶 """
def linear_log_recur(n):
    if n <= 1: return 1
    count = linear_log_recur(n // 2) + \
            linear_log_recur(n // 2)
    for _ in range(n):
        count += 1
    return count
```
  • 当n为浮点数时,for循环报错
TypeError: 'float' object cannot be interpreted as an integer
  • 我更改了一版,麻烦看一下可以不,可以的话我提交pr
""" 线性对数阶 """
def linear_log_recur(n):
    if n <= 1: return 1
    count = linear_log_recur(n / 2) + \
            linear_log_recur(n / 2)
    i = 0
    while i < n:
        count += 1
        i += 1
    return count
@krahets
Copy link
Owner

krahets commented Dec 13, 2022

哈喽,谢谢指正!这样可能所有带 for 循环的都要改下,range() 只允许传入整数,我打算做一下 int() 处理。

@krahets krahets closed this as completed Dec 13, 2022
@Liuyanv
Copy link

Liuyanv commented Mar 21, 2024

please reopen due issue has not been resolved yet

@krahets
Copy link
Owner

krahets commented Mar 22, 2024

please reopen due issue has not been resolved yet

Sorry that I missed it. Fixed in #1164 (Use int instead)

def linear_log_recur(n: int) -> int:
    """线性对数阶"""
    if n <= 1:
        return 1
    count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)
    for _ in range(n):
        count += 1
    return count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants