Accepted java Solution


  • 0
    B
    public class Solution {
        public String convert(String s, int nRows) {
            if (s.length()<=1 || nRows==1) return s;
            StringBuilder builder = new StringBuilder("");
            int unit = 2 * nRows - 2;
            int tmp;
            for (int i = 0; i < nRows; i++) {
                tmp = unit - i * 2;
                int index = i;
                while (index < s.length()) {
                    builder.append(s.charAt(index));
                    if (i>0 && i<nRows-1) {
                        index += tmp;
                        tmp = unit - tmp;
                    } else {
                        index += unit;
                    }
                }
            }
            return builder.toString();
        }
    }

Log in to reply
 

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