开发者

How can using strings instead of simple types like integers alter the O-notation of operations?

Proposed answer:

Strings are simply arrays of 开发者_如何学Pythoncharacters so the O-notation will be dependent on the number of characters in the string (if the loop depends on the length of the string). In this case the O-notation wouldn't be affected because the length of the string is a constant.

Any other ideas? Am I reading this question correctly?


This is not true, since representing integers in arrays are not boundless.

IOW a string that represents an 32-bit integer is maximally 32-bit, thus maximally 10 digits in base 10, and O(10) is a negiable constant that doesn't change the O notation.

So, in summary, while strings are O(n), basic integer types represented as strings are O(maximally 10)=O(0)

I think you need to specify your problem better.


Try thinking about something that operates on an array of integers or an array of strings, clearly in the latter case you have an array of array of a primitive type rather than an array of a primitive type. How does this change things?


That depends entirely on what you are doing with the strings.

If you for example copy items from one array to another, the result is depending on the implementation. It's still an O(n) operation, but the meaning of n changes. If copying a string causes a new copy to be created, n means the total number of characters in all the strings. If copying a string is only copying the reference to it, n means the total number of strings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜