简体   繁体   English

Python 中的时间复杂度 str.isdigit() 是多少?

[英]What is time complexity str.isdigit() in Python?

如果我想知道其他内置函数的时间复杂度,除了找cheatsheet之外还有什么方法可以解决

Think about what you're actually trying to do, verifying that the string is a digit.想一想您实际上要做什么,验证字符串是否为数字。 The only way to be 100% sure about that is by making sure every character in the string is a digit, therefore it needs to evaluate every character. 100% 确定这一点的唯一方法是确保字符串中的每个字符都是数字,因此它需要评估每个字符。

Therefore it has a linear time complexity, O(n), where n is the length of the string因此它具有线性时间复杂度 O(n),其中 n 是字符串的长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM