如何使用优先级队列实现堆栈?(How to Implement stack using priority queue?)
如何使用优先级队列实现堆栈?
伙计们,这是微软面向软件工程师/开发人员的面试问题 。我无法弄清问题的含义。于是我瞪着眼睛,发现了这个:
堆栈和队列可以建模为特定种类的优先级队列。 在一个堆栈中,每个插入元素的优先级是单调递增的; 因此插入的最后一个元素始终是第一个被检索的元素。
因此,这个问题希望我们做什么。作为栈(纠正我,如果我错了)隐式实现为优先级队列(优先级随着元素添加单调递增)。
有没有人可以弄清楚这个问题的含义。当面试中提出这样的问题时,我们应该做什么。
How to Implement stack using priority queue?
Guys this is a Microsoft Interview Question for Software Engineer/Developer.I just can't make out the meaning of the question.So I goggled and found this:
Stacks and queues may be modeled as particular kinds of priority queues. In a stack, the priority of each inserted element is monotonically increasing; thus, the last element inserted is always the first retrieved.
So what this question wants us to do.As stacks (Correct me if am wrong) are implicitly implemented as priority queues (priority being monotonically increasing as elements are added).
Does anybody can make out the meaning of this question.What we are supposed to do when such type of question is asked in an interview.
原文:https://stackoverflow.com/questions/4873472
最满意答案
首先你必须创建一个X类的对象(外部类),然后使用
objX.new InnerClass()
语法来创建一个Y类的对象。尝试,
X x=new X(); X.Y y=x.new Y();
First of all you have to create an object of X class (outer class) and then use
objX.new InnerClass()
syntax to create an object of Y class.Try,
X x=new X(); X.Y y=x.new Y();
相关问答
更多-
我怎样才能解决“一个包含XY的封闭实例是必需的”?(How can I resolve “an enclosing instance that contains X.Y is required”?)[2023-01-24]
首先你必须创建一个X类的对象(外部类),然后使用objX.new InnerClass()语法来创建一个Y类的对象。 尝试, X x=new X(); X.Y y=x.new Y(); First of all you have to create an object of X class (outer class) and then use objX.new InnerClass() syntax to create an object of Y class. Try, X x=new X(); X.Y ... -
你试图使用非静态内部positionObj类而没有一个Secretary的实例属于它。 非静态内部类必须属于其父类的实例 您应该将positionObj更改为普通类或静态内部类。 或者,您可以编写someSecretary.new positionObj()来创建属于someSecretary实例的内部类的实例。 You're trying to use the non-static inner positionObj class without an instance of Secretary for i ...
-
必须使用GeoLocation类型的封闭实例来限定分配(Must qualify the allocation with an enclosing instance of type GeoLocation)[2023-04-18]
嗨,我找到了一个解决方案;-) 发生此错误是因为您正在尝试创建内部类service.submit(new ThreadTask(i));的实例service.submit(new ThreadTask(i)); 没有创建主类的实例 要解决这个问题,请先创建一个main类的实例: GeoLocation outer = new GeoLocation(); 然后创建你打算调用的类的实例,如下所示: service.submit(outer.new ThreadTask(i)); 我希望这将解决你的问题;- ... -
您必须在创建内部类的实例之前首先创建mProgram的实例,或者您可以将内部类(在这种情况下为NrlData)声明为静态,但您仍然需要mProgram类来访问它(但是您不需要必须实例化它。 public class mProgram { public class NrlData { ... } public static void main(String[] args) { mProgram.NrlData nrlData = new mProgra ...
-
尝试这个。 删除了简单的方法 public class Test1 { public static void main( String [] args) { MyTriangle h1 = new MyTriangle(); } } class MyTriangle implements Triangle{ int side1; int side2; int side3; public MyTriangl ...
-
如何可以(xy!= null &&((object)xy)== null)是真的吗?(How can (x.y != null && ((object)x.y) == null) ever be true?)[2023-09-19]
发现错误: public static bool operator ==(Identifier A, Identifier B) { if (ReferenceEquals(A, null)) return false; return A.Equals(B); } 如果B为null会发生什么? A == B应该返回true ,可悲的是你的比较将return false ; 将其更改为: if (ReferenceEquals(A, null)) return ReferenceEq ... -
我的猜测是CustomListAdapter没有被定义为static内部类,因此不能从另一个static内部类中使用它。 另外,请摆脱构造函数,或至少链接到超类构造函数。 My guess is that CustomListAdapter is not defined as a static inner class, and therefore it cannot be used from another static inner class. Also, please get rid of the co ...
-
这是因为SAT类是BoardTestBean的内部类 ,但不是静态的内部类 。 只有静态的内部类可以在没有“封闭”实例上下文的情况下实例化; 非静态需要一个“父”实例。 如果SAT不需要使用任何BoardTestBean的状态,则声明它是static ; 否则,将一个实例方法添加到BoardTestBean并从那里实例化SAT 。 PS我假设您从同一个包中访问SAT ,因为它具有包可见性 。 如果这不是故意的,您还需要将该类public 。 编辑这是向BoardTestBean添加实例方法返回SAT方法: ...
-
否包含实例错误(No Enclosing instance error)[2022-02-13]
这是因为您尝试从静态方法实例化非静态内部类。 Java有两种可以嵌套在类级别的内部类 - 静态和非静态。 非静态类具有对其“外部”类的实例的引用,在其中实例化它们。 这允许非静态内部类访问其外部类的实例变量。 静态类(例如HelloComponent2 )不访问其外部类的实例变量。 这允许您从静态函数实例化此类。 使类static应该解决问题: static class HelloComponent2 extends JComponent implements MouseMotionListener Th ... -
BluetoothSerialService是内部类吗? 如果是这样,请将其static 。 看这篇文章 不能访问类型为Server的封闭实例 Is the BluetoothSerialService an inner class? If so, make it static. See this post No enclosing instance of type Server is accessible