easy java solution


  • 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();
    }
    

    }

    '''


Log in to reply
 

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