这些堆栈需要合并吗?
在考试 98-361 软件开发基础知识的能力评估中,会出现以下问题:
场景 3-3:使用堆栈
您正在编写一个使用两个堆栈的程序。每个堆栈中的数据已经按降序排列。您需要以这样的方式处理两个堆栈的内容,即输出按升序打印在屏幕上。你会如何编写这样的程序?
现在,我已经对这个场景进行了编码。我的解决方案是迭代两个单独的堆栈,通过弹出它们的项目将它们合并到列表中,直到堆栈为空,然后将列表按正确的顺序排序。
然而,令我震惊的是,关于我是否应该合并堆栈的问题有点模糊。这是某种的暗示,但也某种不是。
如果您正在阅读这个问题,您会如何解释它?
请注意,我实际上并没有参加这次考试,只是在准备考试。在我看来,这更多的是一个需求解释问题。
In a proficiency assessment as part of Exam 98-361, Software Development Fundamentals, this question pops up:
Scenario 3-3: Using Stacks
You are writing a program that uses two stacks. The data in each stack is already in descending order. You need to process the contents of both stacks in such a way that the output is printed on the screen ascending order. How would you write such a program?
Now, I have this scenario already coded. My solution is to iterate over two separate stacks, merge them into a List by popping their items off until the stack is empty, and sort the list into the correct order.
However, it strikes me that the question is a bit vague on whether or not I should be merging the stacks. It's kind of implied, but it kind of isn't.
If you were reading this question, how would you interpret it?
Note that I'm not actually taking this exam, just prepping for it. It's more of a requirements interpretation issue, at this point, in my mind.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的观点:
现在您已经排序了
NEW_LIST
- 您只需要决定打印顺序(升序相反)
假设 m, n 是两个堆栈的初始大小,
合并两个堆栈然后对列表进行排序将花费更多 -
O((n+m)log (n+m))
用于快速排序,这显然比O(m+n)
慢 - 对于上面的解决方案My point of view:
Now you have
NEW_LIST
which is sorted - you just need to decide on the printing order(reverse for ascending order)
Assuming
m, n
are the initial sizes of the two stacks,Merging both stacks and then sorting a list will cost you more -
O((n+m)log(n+m))
for a quicksort, which is obviously slower thanO(m+n)
- for the solution above我认为你是对的。这是我最初的解释(尽管我同意这似乎有点模糊)。经过进一步的思考,在我看来,没有必要指定两个堆栈,除非它们应该被合并。
I think you're correct. That was my initial interpretation (although I agree it seems a bit vague). And on further thought, it seems to me there'd be no need to specify two stacks unless they were supposed to be merged.
这个问题听起来像是一个合并排序的设置;要以组合、排序(但相反)的顺序获取两者的内容,您需要重复查看每个堆栈的顶部以查看哪个值较低(id est,按降序排序较早),将值从该堆栈中弹出,然后将其推入第三个堆栈。重复此操作,直到两个源堆栈都为空,并且因为您的第三个堆栈是 FILO,所以您按降序添加的项目将按升序弹出。
This question sounds like a set-up for a merge-sort; to get the contents of both in a combined, sorted (but reversed) order, you repeatedly peek the top of each stack to see which value is lower (id est, earlier in a descending order sort), pop the value off that stack and push it to a third stack. Repeat until both source stacks are empty, and because your third stack is FILO the items you added in descending order will pop off in ascending order.