Why my code is output limit exceeded?


  • 0
    Z

    class Solution {
    public:
    string convert(string s, int nRows) {

        string res="";
        int n=2*(nRows-1);
        if(nRows==1)
        return res;
        
        
        for(int i=0;i<nRows;i++){
            int index=i,flag=0;
            while(index<s.size()){
                res.append(1,s[index]);
                if(i==0||i==nRows-1)
                index+=n;
                else{
                    if(flag==0){
                    index+=n-2*i;
                    flag=1;
                    }
                    else
                    index+=2*i;
                    flag=0;
                }
                if(index>s.size())
                break;
                
            }
            
        }
        return res;
        
    }
    

    };


Log in to reply
 

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