//13 ms 39.4 MB public String convert(String s, int numRows) { if (numRows == 1) { return s; } String[] strings1 = new String[numRows]; for (int i = 0; i < numRows; i++) { strings1[i] = ""; } int len = s.length(); int per = 2 * numRows - 2; LinkedList<ArrayList<String>> ll = new LinkedList<>(); ArrayList<String> arrayList = null; ArrayList<String> arrayList2 = null; for (int i = 0; i < len; i++) { int left = i % per; int left2 = left - numRows; int n = (left / numRows); if (left == 0) { arrayList = new ArrayList<>(); ll.add(arrayList); } if (left2 == 0) { arrayList2 = new ArrayList<String>(Arrays.asList(strings1)); // for (int j = 0; j < numRows; j++) { // arrayList2.add(""); // } ll.add(arrayList2); } if (n < 1) { arrayList.add(s.charAt(i) + ""); } else if (n == 1 && arrayList2 != null) { arrayList2.set(numRows - 1 - 1 - left2, s.charAt(i) + ""); } } StringBuilder stringBuilder = new StringBuilder(); for (int m = 0; m < numRows; m++) { for (int n = 0, size = ll.size(); n < size; n++) { ArrayList<String> strings = ll.get(n); stringBuilder.append(m >= strings.size() ? "" : strings.get(m)); } } return stringBuilder.toString(); } 复制代码
以上所述就是小编给大家介绍的《每3日一篇leetcode,第五题:ZigZagConversion》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Learning Web App Development
Semmy Purewal / O'Reilly Media / 2014-3-3 / USD 29.99
Grasp the fundamentals of web application development by building a simple database-backed app from scratch, using HTML, JavaScript, and other open source tools. Through hands-on tutorials, this pract......一起来看看 《Learning Web App Development》 这本书的介绍吧!