[英]What is the current size of Stack?
Suppose an initially empty stack S has performed a total of 25 push operations, 12 top operations, and 10 pop operations, 3 of which returned null to indicate an empty stack . 假设最初为空的堆栈S总共执行了25个推入操作,12个顶部操作和10个弹出操作,其中3个返回null表示空堆栈 。 What is the current size of S?
S的当前大小是多少?
I'm thinking that S.size =7 because 10 pop operations have 3 out of 10 returned null to indicate an empty stack but not really sure if it's correct or not 我在想S.size = 7,因为10个弹出操作中有10个弹出操作中有3个返回null来表示一个空堆栈,但不能真正确定它是否正确
Can any one give the correct answer and explanation ? 谁能给出正确的答案和解释?
10-3 = 7
elements, since 3 of the pops did not change the state (and size) of the stack, so only 7 pops did. 10-3 = 7
元素,因为其中3个弹出窗口不会更改堆栈的状态(和大小),因此只有7个弹出窗口可以更改。 Total size of the stack is 25-7 = 18
at the end. 最后,堆栈的总大小为
25-7 = 18
。
Note that the order of operations does not matter, only the amount of "succesfull" pop()
s, and amount of push()
s. 请注意,操作顺序并不重要,仅“ succesfull”
pop()
的数量和push()
的数量无关紧要。
25 pushes = 25 index 25次推送= 25次索引
10 pops = 25 - 10 = 15 10次弹出= 25-10 = 15
3 pops did not occur = 15 + 3 = 18 未发生3声啪啪声= 15 + 3 = 18
tops does not matter, so it should be 18 上衣无所谓,所以应该是18
它是推入次数和弹出次数之间的差。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.