JAVA zigzag(easy)


  • 0
    D

    it's a recursion every 2n-2 char and in once zig and zag,n-1 is the middle index.
    so my code is following:
    '''
    class Solution {
    public String convert(String s, int numRows) {
    if(numRows==1){
    return s;
    }
    StringBuffer[] sb=new StringBuffer[numRows];
    StringBuffer result=new StringBuffer();
    for(int i=0;i<numRows;i++){
    sb[i]=new StringBuffer();
    }
    for(int i=0;i<s.length();i++){
    int index=i%(2*numRows-2);
    int index1=numRows-Math.abs(index-(numRows-1))-1;
    sb[index1].append(s.charAt(i));
    }
    for(int i=0;i<numRows;i++)
    result.append(sb[i]);
    return result.toString();
    }
    }
    '''


Log in to reply
 

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