11 lines simple Java solution, beats 86%


  • 0
    H

    public class Solution {

    public String convert(String s, int numRows) {
        if(s.length() <= 1 || numRows == 1) return s;
        int len = s.length();
        StringBuilder res = new StringBuilder("");    
        for(int i = 0; i < numRows; i++){
            for(int j = 0; j+i < len; j += 2 * numRows - 2){
                res.append(s.charAt(j+i));
                int tmp = j+numRows-1+(numRows-1-i);
                if(i > 0 && i < numRows-1 && tmp < len)    res.append(s.charAt(tmp));
            }
        }
        return res.toString();
    }
    

    }


  • 0
    H
    This post is deleted!

Log in to reply
 

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