public class Solution {
   public String convert(String s, int nRows) {
        if (s == null || s.isEmpty() || s.length() <= nRows || nRows == 1) {
            return s;
        }

        int length = s.length();

        StringBuilder sb = new StringBuilder();

        int step = 2 * (nRows - 1);
        int count = 0;

        for (int i = 0; i < nRows; i++){
            int interval = step - 2 * i;

            for (int j = i; j < length; j += step){
                   sb.append(s.charAt(j));
                   count++;
                   if (interval > 0 && interval < step && j + interval < length && count <  length) {
                        sb.append(s.charAt(j + interval));
                        count++;
                }
            }
        }
        return sb.toString();
    }
}

相关文章:

  • 2021-08-18
  • 2021-09-23
  • 2021-06-03
  • 2022-03-04
  • 2021-07-30
  • 2022-02-22
猜你喜欢
  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-05-22
  • 2021-11-29
相关资源
相似解决方案