//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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Pro JavaScript Techniques
John Resig / Apress / 2006-12-13 / USD 44.99
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web s......一起来看看 《Pro JavaScript Techniques》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
html转js在线工具
html转js在线工具