Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя i_am_eating_wa_and_tle

Автор i_am_eating_wa_and_tle, история, 7 месяцев назад, По-английски

As far as I know stack.push() have a O(1) time complexity but is it actually correct if I use a stack of string.

stack<string> stringStack;
string inputString;
while(cin >> inputString) {
    stringStack.push(inputString); // what is the time complexity of this line
}
  • Проголосовать: нравится
  • +15
  • Проголосовать: не нравится

»
7 месяцев назад, # |
  Проголосовать: нравится -23 Проголосовать: не нравится

It's constant, O(1).

The operation involves updating the reference or pointer to the new element, and this process is independent of the size of the string.