PHP 函数 strlen() 的算法复杂度
最近面试时被问到这个问题,我不知道如何回答。
谁能回答这个问题并描述一下吗?
Recently I was asked this question on interview and I didn't know how to answer it.
Can anyone answer this question and describe it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
O(1),因为长度存储为属性: source
然而,这个琐事值得反驳,通过关于微优化剧场的讨论,由我们的主持人此处 和 此处;阅读这两个链接,无论您是否知道特定答案,您都会找到一个很好的话题,可以在下次出现类似问题时改变对话的势头!
面试官对你的切线的反应会告诉你你有多想和他们一起工作。
O(1) since the length is stored as an attribute: source
However, this trivia is worth countering with a discussion about micro-optimising theatre, as kindly provided by our hosts here and here; read those two links and you'll find a good talking point to change the momentum of the conversation next time similar questions come up, regardless of whether you know the particular answer!
How the interviewer reacts to your tangent will tell you a lot about how much you want to work with them..
我假设该函数的复杂度为 O(n),因为它需要遍历字符串一次。
I would assume that function is O(n) because it would need to iterate through the string once.