Problem:

LeetCode打卡(五)ZigZag Conversation

Solution:

LeetCode打卡(五)ZigZag Conversation

Code:

class Solution {
    public String convert(String s, int numRows) {

        if (numRows == 1) return s;

        StringBuilder ret = new StringBuilder();
        int n = s.length();
        int cycleLen = 2 * numRows - 2;

        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j + i < n; j += cycleLen) {
                ret.append(s.charAt(j + i));
                if (i != 0 && i != numRows - 1 && j + cycleLen - i < n)
                    ret.append(s.charAt(j + cycleLen - i));
            }
        }
        return ret.toString();
    }
}

 

相关文章:

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