ios - 如果 T.Index 是 RandomAccessIndexType,则 countElements 的运行时间复杂度?

标签 ios string swift

计数 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/

相关文章:

java - 比较输入时哈希集给出错误

ios - 在 Swift 中出现键盘时将按钮移动到 View

ios - 如何在 iOS 的 WebView 中打开 URL?

ios - UIDatePicker 不遵守夏令时

ios - 仅在测试目标中使用自定义字体?

ios - 核心蓝牙 : What is the lifetime of unique UUIDs

c# - 如何将逗号分隔的字符串转换为 List<int>

定义的字符数组小于初始化字符串

swift - 单例模式和正确使用 Alamofire 的 URLRequestConvertible

ios - 使用 Parse 在 PFQuery 之外重新调整空白数组的数组。