计数 String
的字符时,countElements 函数的运行时间复杂度是多少?
文档说:
O(1) if T.Index is RandomAccessIndexType; O(N) otherwise.
什么是 RandomAccessIndexType? String
是 RandomAccessIndexType 吗?
最佳答案
根据文档:
An index that can be offset by an arbitrary number of positions, and can measure the distance to any reachable value, in O(1)
Swift 字符串(不幸的是)没有实现它。他们使用 Bi DirectionIndexType 来代替,这基本上使字符串成为双向链表,这意味着为了从位置 m 到达位置 n 处的字符,它浏览它们之间的所有元素。因此,字符串的 countElements
函数的复杂度为 O(n)
关于ios - 如果 T.Index 是 RandomAccessIndexType,则 countElements 的运行时间复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704682/