My Java and Python solution with two for loop easy to understand

  • 0

    Java solution:

    public class Solution {
        public String convert(String s, int numRows) {
            if(numRows<=1 || numRows>=s.length() || s==null)
                return s;
            int zigSpan = (numRows-1)*2;
            String res = "";
            for(int i=0;i<numRows;i++) {
                for(int j=i;j<s.length();j+=zigSpan) {
                    //add every element for each column and the span is zigSpan
                    res += s.charAt(j);
                    //if not the first and last column, just add additional element if not beyond the length of s
                    //and the span is zigSpan+j-2*i
                    if(i!=0 && i!=numRows-1 && zigSpan+j-2*i<s.length()) res += s.charAt(zigSpan+j-2*i);
            return res;


    class Solution:
    # @param {string} s
    # @param {integer} numRows
    # @return {string}
    def convert(self, s, numRows):
        if numRows<=1 or numRows>len(s):
            return s
        zigSpan = 2*(numRows-1)
        res = ''
        for i in range(numRows):
            for j in range(i,len(s),zigSpan):
                res += s[j]
                if i!=0 and i!=numRows-1 and zigSpan+j-2*i<len(s):
                    res += s[zigSpan+j-2*i]
        return res

Log in to reply

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