gpt4 book ai didi

python - python 3中字符串切片操作的时间复杂度

转载 作者:行者123 更新时间:2023-12-02 03:51:43 27 4
gpt4 key购买 nike

Given a string s of length n, the slicing operation s[i : j] in Python 3, where
(0 <=i <= j <= n), takes how much time in Big-O notation?

是 O(n) 还是 O(1) 还是其他?

编辑

Python 3 中列表和字符串的切片是否存在实现差异?

最佳答案

CPython 通过创建一个包含提取字符的新字符串对象来实现字符串切片。所花费的时间与复制的字符数成正比,因此所花费的时间与j-i(复制的字符数)成正比。

关于python - python 3中字符串切片操作的时间复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45198936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com