「リートコード - 6. ZigZag Conversion」 发表于 2021-07-29 | 分类于 ジャバ ジャバソリューション1234567891011121314151617181920212223242526class Solution { public String convert(String s, int numRows) { if (numRows < 2) { return s; } StringBuffer sb = new StringBuffer(); int n = s.length(); int step = numRows * 2 - 2; for (int i = 0; i < n; i += step) { sb.append(s.charAt(i)); } for (int i = 1; i < numRows - 1; ++i) { int cur_step = (numRows - i - 1) * 2; for (int j = i; j < n; j += step) { sb.append(s.charAt(j)); if (j + cur_step < n) { sb.append(s.charAt(j + cur_step)); } } } for (int i = numRows - 1; i < n; i += step) { sb.append(s.charAt(i)); } return sb.toString(); }} 很水模拟,只是居然特判n==1 WA一小会儿。看了眼题解,似乎我这种想法算是冷门的?都是用n个串去分别连。不过反正复杂度都一样。 本文作者: XIAO TUO 本文链接: http://tashi711.top/java/leetcode-6/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!