Hate this problem but check my answer


  • 0
    L

    Pretty straight forward, goes line by line

    public class Solution {
        public String convert(String s, int numRows) {
            if (numRows == 1) {
                return s;
            }        
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= numRows; i++) {
                if (i == 1) {
                    int k = 0;
                    while (k * (numRows * 2 -2) < s.length()) {
                        sb.append(s.charAt(k * (numRows * 2 -2)));
                        k ++;
                    }
                } else if (i == numRows) {
                    int k = 0;
                    while (k * (numRows * 2 -2) + numRows - 1 < s.length()) {
                        sb.append(s.charAt(k * (numRows * 2 -2) + numRows - 1));
                        k ++;
                    }
                } else  {
                    int k = 0;
                    while ((k * (numRows * 2 -2) + i - 1 < s.length())) {
                        sb.append(s.charAt(k * (numRows * 2 -2) + i - 1));
                        if ((k + 1) * (numRows * 2 -2) - i + 1 < s.length()) {
                            sb.append(s.charAt((k + 1) * (numRows * 2 -2) - i + 1 ));
                        }
                        k ++;
                    }
                }
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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