Memory Limit Exceed


  • 0
    X

    The following is my code for ZigZag conversion question. My code run perfectly on my local computer. But when it comes to the online judge, it gives me memory limit exceed error. Any one can help?

    public class Solution{
        public String convert(String s, int nRows) {
            
            StringBuffer zigzag=new StringBuffer();
            int length=s.length();
            
            for (int j=0;j<=(nRows-1);j++){
                int interval=2*nRows-2;
                for (int i=j;i<length;i=i+interval){
                    zigzag.append(s.charAt(i));
                    int interval2=interval-2*j+i;
                    if (!(j==0||j==(nRows-1))&& interval2<length){
                        zigzag.append(s.charAt(interval2));
                    }
                }
            }
            
            return zigzag.toString();
            
        }
    }

  • 0
    S

    Could you please correct code format? Thanks.


  • 0
    X

    done. Thanks!


  • 8
    S

    Your code will go to dead loop when nRows equals 1, then the zigzag strong will have infinite characters. However, seems it first hit the memory limit of OJ.


Log in to reply
 

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