【发布时间】:2014-09-23 22:23:45
【问题描述】:
这个循环的时间复杂度是多少,因为它不迭代 1:
while (parser.hasNext())
{
token = parser.next();
if (isOperator(token))
{
op2 = (String)(stack.pop());
op1 = (String)(stack.pop());
result = evaluateSingleOperator(token.charAt(0), op1, op2);
stack.push(result);
}
else
stack.push(token);
}
return result;
会不会是 O(n),因为如果有 5 个元素,h e l l o,那么循环内的语句将运行 5 次?
【问题讨论】:
-
它是
O(n),其中 n 是您的parser中tokens的数量。
标签: java time complexity-theory