easy java solution


  • 0
    L

    //1 思路判断字符位置的相对位置
    public static String convert(String s, int numRows) {
    if (s == null || s.length() < 2 || numRows < 2) return s;
    StringBuilder sb = new StringBuilder();
    for (int i = 0;i < numRows;i++) {
    boolean flag = true;
    for (int j = i;j < s.length();) {
    sb.append(s.charAt(j));
    //起始和结尾行的处理
    if (i == numRows - 1 || i == 0) {
    j = j + (numRows - 1) * 2;
    } else {
    //朝下移
    if (flag) {
    j = j + (numRows - i - 1) * 2;
    flag = false;
    } else {
    //朝上移
    j = j + i * 2;
    flag = true;
    }
    }
    }
    }
    return sb.toString();
    }


Log in to reply
 

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