PHP 函数 strlen() 的算法复杂度

发布于 2024-10-21 14:27:18 字数 53 浏览 6 评论 0原文

最近面试时被问到这个问题,我不知道如何回答。

谁能回答这个问题并描述一下吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-10-28 14:27:18

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..

花开半夏魅人心 2024-10-28 14:27:18

我假设该函数的复杂度为 O(n),因为它需要遍历字符串一次。

I would assume that function is O(n) because it would need to iterate through the string once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文