Very easy-understand Java solution, No array, just O(n)Space and O(n)Time


  • 0
    X

    '''
    public class Solution {
    public String convert(String s, int numRows) {
    if(numRows == 1)return s;
    StringBuilder sb = new StringBuilder();
    int n = 2 * numRows - 2, m = n;
    for(int i = 0; i < numRows; i++){//add numbers row by row
    int j = i;
    while(j < s.length()){//add each row
    sb.append(s.charAt(j));
    if(i != 0 && i != numRows - 1 && j != i){
    if(j + m - n < s.length()){
    j += m - n;
    sb.append(s.charAt(j));
    }
    else break;//the end of current row
    }
    if(i == numRows - 1)j += m;
    else j += n;
    }
    n -= 2;
    }
    return sb.toString();
    }
    }
    '''


  • 0
    C

    @xuanbryantgmail.com ugly format


Log in to reply
 

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