leetcode-6. Z 字形变换

  • Post author:
  • Post category:其他


class Solution {
	public String convert(String s, int numRows) {
		if (s.length() == 1)
			return s;
		StringBuffer[] sbs = new StringBuffer[numRows];
		for (int i = 0; i < sbs.length; ++i)
			sbs[i] = new StringBuffer();
		int i = 0;
		while (i < s.length()) {
			for (int j = 0; j < numRows && i < s.length(); ++j) {
				sbs[j].append(s.charAt(i++));
			}
			for (int j = numRows - 2; j > 0 && i < s.length(); --j) {
				sbs[j].append(s.charAt(i++));
			}
		}
		for (int j = 1; j < numRows; ++j)
			sbs[0].append(sbs[j]);
		return sbs[0].toString();
	}
}



版权声明:本文为qq_39370495原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。