Algorithmic complexity of PHP function strlen()

爷,独闯天下 提交于 2019-11-28 07:00:08

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/5292325/algorithmic-complexity-of-php-function-strlen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!