who can tell me why,really confused


  • -1
    S

    public class Solution {
    public int maximalRectangle(char[][] matrix) {
    if (matrix == null || matrix.length == 0) {
    return 0;
    }
    int max=0;
    int length=matrix[0].length;
    int []height=new int[length];
    for(int i=0;i<matrix.length;i++){
    for(int j=0;j<length;j++){
    if(matrix[i][j]==0) height[j]=0;
    else height[j]=height[j]+1;

                // height[j]=matrix[i][j]==0?0:(height[j]+1);
            }
            max=Math.max(max,maxRec(height));
        }
     return max;   
    }
    public int maxRec(int []height){
        if(height==null||height.length==0)  
            return 0;
        int max=0;
        Stack<Integer> stack=new Stack<>();
        for(int i=0;i<height.length;i++){
            while(!stack.isEmpty()&&height[i]<=height[stack.peek()]){
                int j=stack.pop();
                int k=stack.isEmpty()?-1:stack.peek();
                int curArea=(j-k+1)*height[j];
                max=Math.max(curArea,max);
            }
            stack.push(i);
        }
        while(stack.isEmpty()){
           int j=stack.pop();
           int k=stack.isEmpty()?-1:stack.peek();
           int curArea=(j-k+1)*height[j];
           max=Math.max(curArea,max);
        }
        return max;
    }
    

    }

    I debug my code in IDEA, it runs correctlly.But in leetcode ,it failed;
    0_1479867100612_upload-7fd7fe95-8f93-4cec-b6ec-86a67212d4a5
    ![0_1479867151848_upload-b31e4aa9-4069-49ab-b8b2-c72c7ecc8539]
    (/uploads/files/1479867149624-upload-b31e4aa9-4069-49ab-b8b2-c72c7ecc8539.png)
    it return all ones


  • 0
    S

    @simple008 said in who can tell me why,really confused:

    public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0) {
            return 0;
        }
        int max=0;
        int length=matrix[0].length;
        int []height=new int[length];
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<length;j++){
                if(matrix[i][j]==0) height[j]=0;
                else height[j]=height[j]+1; 
                // height[j]=matrix[i][j]==0?0:(height[j]+1);
            }
            max=Math.max(max,maxRec(height));
        }
     return max;   
    }
    public int maxRec(int []height){
        if(height==null||height.length==0)  
            return 0;
        int max=0;
        Stack<Integer> stack=new Stack<>();
        for(int i=0;i<height.length;i++){
            while(!stack.isEmpty()&&height[i]<=height[stack.peek()]){
                int j=stack.pop();
                int k=stack.isEmpty()?-1:stack.peek();
                int curArea=(j-k+1)*height[j];
                max=Math.max(curArea,max);
            }
            stack.push(i);
        }
        while(stack.isEmpty()){
           int j=stack.pop();
           int k=stack.isEmpty()?-1:stack.peek();
           int curArea=(j-k+1)*height[j];
           max=Math.max(curArea,max);
        }
        return max;
    }
    

    }

    I debug my code in IDEA, it runs correctlly.But in leetcode ,it failed;
    0_1479867100612_upload-7fd7fe95-8f93-4cec-b6ec-86a67212d4a5
    0_1479867332230_upload-bbccf26e-5cdc-422f-b398-4adaee0bec38
    it return all ones


  • 0

    @simple008 The first thing you need to do before posting a question is read the instructions at the head of the page and format your problem properly.

    Thank you!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.