Java ArrayQue push()似乎添加到了堆栈的前端/后端[潜在的bug]
我正在研究leetcode问题84,最大矩形。在测试时,我遇到了一个奇怪的情况,堆栈似乎添加到了尾部。我使用print语句和迭代器对象确认了这一点 测试用例为:[4,2,0,3,2,5] 数组中倒数第二个元素2似乎被推到尾部,就在0的正下方(它应该被推到顶部。在我的print语句中,val:x gap:y在弹出元素时出现,x y z在推元素时出现,并添加:x是迭代器打印的内容。整个堆栈在数组中的每一个增量都会被迭代。代码在这里。我确信像这样发布一个代码块是不礼貌的,所以感觉fre我想提出一些批评Java ArrayQue push()似乎添加到了堆栈的前端/后端[潜在的bug],java,debugging,stack,arraydeque,c,c++,c#,python,javascript,go,rust,Java,Debugging,Stack,Arraydeque,C,C++,C#,Python,Javascript,Go,Rust,我正在研究leetcode问题84,最大矩形。在测试时,我遇到了一个奇怪的情况,堆栈似乎添加到了尾部。我使用print语句和迭代器对象确认了这一点 测试用例为:[4,2,0,3,2,5] 数组中倒数第二个元素2似乎被推到尾部,就在0的正下方(它应该被推到顶部。在我的print语句中,val:x gap:y在弹出元素时出现,x y z在推元素时出现,并添加:x是迭代器打印的内容。整个堆栈在数组中的每一个增量都会被迭代。代码在这里。我确信像这样发布一个代码块是不礼貌的,所以感觉fre我想提出一些批评
class Solution {
public int largestRectangleArea(int[] heights) {
//use a stack
//if element is bigger than top of stack, than add element to stack
//if element is same as top, add element to stack
//if element is less than top, pop all elements and calculate areas, also keep track of area of new top
Deque<Helper> myStack = new ArrayDeque<Helper>();
if (heights.length == 0 || heights == null) return 0;
if (heights.length == 1) return heights[0];
int poppedLength = 0;
int area;
int maxArea = 0;
Helper previous = new Helper(heights[0]);
myStack.push(previous);
for (int i = 1; i < heights.length; i++) { //iterate through input array
Iterator<Helper> myIt = myStack.iterator();
while (myIt.hasNext()) { //iterate through stack, for testing purposes
System.out.print(myIt.next().toString());
System.out.println();
}
if (!myStack.isEmpty()) {
if (heights[i] >= myStack.peek().getValue()) {//if curr element is greater than last, push current element
myStack.push(new Helper(heights[i]));
System.out.print("added1: "); //testing print statements
System.out.println(heights[i]);
} else {
while (heights[i] < myStack.peek().getValue()) { //if current element is less than head of stack, pop elements from stack until current is >= head of stack
Helper popped = myStack.pop();
poppedLength++;
area = (poppedLength + popped.getGapLength()) * popped.getValue();
System.out.print(poppedLength + popped.getGapLength()); //print statements for testing
System.out.print(" ");
System.out.print(popped.getValue());
System.out.print(" ");
System.out.print(area);
System.out.println();
if (area > maxArea) maxArea = area; //update max
if (myStack.isEmpty()) break;
}
if (!myStack.isEmpty()) {
myStack.peek().setGapLength(poppedLength + myStack.peek().getGapLength());
}
myStack.add(new Helper(heights[i], poppedLength)); //push current, THIS IS WHERE THE ERROR IS OCCURING
System.out.print("added2: ");
System.out.println(heights[i]);
poppedLength = 0;
}
} else {//if stack is empty for some reason, this actually should never execute
myStack.push(new Helper(heights[i]));
}
}
while (!myStack.isEmpty()) {//remove rest of elements in the stack
Helper popped = myStack.pop();
poppedLength++;
area = (poppedLength + popped.getGapLength()) * popped.getValue();
if (area > maxArea) maxArea = area;
System.out.print(poppedLength + popped.getGapLength());
System.out.print(" ");
System.out.print(popped.getValue());
System.out.print(" ");
System.out.print(area);
System.out.println();
}
return maxArea;
}
class Helper {//the elements of the stack
private int value;
private int gapLength;
public Helper(int val) {
value = val;
gapLength = 0;
}
public Helper(int val, int gap) {
value = val;
gapLength = gap;
}
public int getValue() {
return value;
}
public int getGapLength() {
return gapLength;
}
public void setGapLength(int length) {
gapLength = length;
}
public String toString() {
String retStr = "Val: " + Integer.toString(value) + " Gap:" + Integer.toString(gapLength) + " ";
return retStr;
}
}
}
类解决方案{
公共int最大矩形区域(int[]高度){
//使用堆栈
//若元素大于堆栈顶部,则将元素添加到堆栈中
//若元素和顶部相同,则将元素添加到堆栈中
//若元素小于顶部,则弹出所有元素并计算面积,同时跟踪新顶部的面积
Deque myStack=new ArrayDeque();
如果(heights.length==0 | | heights==null)返回0;
如果(heights.length==1)返回高度[0];
int poppedlelength=0;
内部区域;
int maxArea=0;
上一个辅助对象=新辅助对象(高度[0]);
myStack.push(上一个);
对于(inti=1;i=myStack.peek().getValue()){//如果curr元素大于last,则推送当前元素
myStack.push(新助手(高度[i]);
System.out.print(“added1:”;//测试打印语句
系统输出打印Ln(高度[i]);
}否则{
而(heights[i]=堆栈头
Helper popped=myStack.pop();
poppedLength++;
area=(poppedLength+popped.getGapLength())*popped.getValue();
System.out.print(poppedLength+popped.getGapLength());//打印测试语句
系统输出打印(“”);
System.out.print(popped.getValue());
系统输出打印(“”);
系统输出打印(面积);
System.out.println();
如果(area>maxArea)maxArea=area;//更新最大值
如果(myStack.isEmpty())中断;
}
如果(!myStack.isEmpty()){
myStack.peek().setGapLength(poppedLength+myStack.peek().getGapLength());
}
add(newhelper(heights[i],poppedLength));//推送当前,这就是错误发生的地方
系统输出打印(“添加2:”);
系统输出打印Ln(高度[i]);
poppedLength=0;
}
}否则{//若堆栈因某种原因为空,则实际上不应执行此操作
myStack.push(新助手(高度[i]);
}
}
而(!myStack.isEmpty()){//则删除堆栈中的其余元素
Helper popped=myStack.pop();
poppedLength++;
area=(poppedLength+popped.getGapLength())*popped.getValue();
如果(面积>最大面积)最大面积=面积;
System.out.print(poppedLength+popped.getGapLength());
系统输出打印(“”);
System.out.print(popped.getValue());
系统输出打印(“”);
系统输出打印(面积);
System.out.println();
}
返回最大区域;
}
类帮助程序{//堆栈的元素
私有int值;
私人内部间隙;
公共助理(int val){
值=val;
gapLength=0;
}
公共助手(int val,int gap){
值=val;
gapLength=间隙;
}
public int getValue(){
返回值;
}
public int getGapLength(){
回程间隙;
}
公共void setGapLength(整数长度){
gapLength=长度;
}
公共字符串toString(){
String retStr=“Val:”+Integer.toString(value)+“Gap:”+Integer.toString(gapLength)+”;
返回retStr;
}
}
}
通过将问题分解为多个函数来解决问题的方法很好。但是,我们很难调试
这将通过:
class Solution {
public static int largestRectangleArea(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int[] leftReduce = new int[height.length];
int[] rightReduce = new int[height.length];
rightReduce[height.length - 1] = height.length;
leftReduce[0] = -1;
for (int i = 1; i < height.length; i++) {
int p = i - 1;
while (p >= 0 && height[p] >= height[i]) {
p = leftReduce[p];
}
leftReduce[i] = p;
}
for (int i = height.length - 2; i >= 0; i--) {
int p = i + 1;
while (p < height.length && height[p] >= height[i]) {
p = rightReduce[p];
}
rightReduce[i] = p;
}
int maxArea = 0;
for (int i = 0; i < height.length; i++) {
maxArea = Math.max(maxArea, height[i] * (rightReduce[i] - leftReduce[i] - 1));
}
return maxArea;
}
}
类解决方案{
公共静态int最大矩形区域(int[]高度){
if(height==null | | height.length==0){
返回0;
}
int[]leftReduce=新int[height.length];
int[]rightReduce=新int[height.length];
rightReduce[height.length-1]=height.length;
leftReduce[0]=-1;
对于(int i=1;i=0&&height[p]>=height[i]){
p=leftReduce[p];
}
leftReduce[i]=p;
}
对于(int i=height.length-2;i>=0;i--){
Deque<Helper> myStack = new ArrayDeque<Helper>();