标签:
/* * 6. ZigZag Conversion * 2016-8-26 by Mingyang * 觉得这个题目面试的必要性不大,有规律,第一行和最后一行每两个相差2n-2 * 中间的每行除了2n-2有外,中间的一个也有2 * (n - 1 - i) */ public String convert(String s, int numRows) { if (numRows == 1) return s; StringBuilder sb = new StringBuilder(); // step int step = 2 * numRows - 2; for (int i = 0; i < numRows; i++) { //first & last rows if (i == 0 || i == numRows - 1) { for (int j = i; j < s.length(); j = j + step) { sb.append(s.charAt(j)); } //middle rows } else { int j = i; boolean flag = true; int step1 = 2 * (numRows - 1 - i); int step2 = step - step1; while (j < s.length()) { sb.append(s.charAt(j)); if (flag) j = j + step1; else j = j + step2; flag = !flag; } } } return sb.toString(); }
标签:
原文地址:http://www.cnblogs.com/zmyvszk/p/5816515.html