|
| 1 | +package pp.arithmetic.all; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by wangpeng on 2019-03-18. |
| 5 | + * 6. Z 字形变换 |
| 6 | + * <p> |
| 7 | + * 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 |
| 8 | + * <p> |
| 9 | + * 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: |
| 10 | + * <p> |
| 11 | + * L C I R |
| 12 | + * E T O E S I I G |
| 13 | + * E D H N |
| 14 | + * 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。 |
| 15 | + * <p> |
| 16 | + * 请你实现这个将字符串进行指定行数变换的函数: |
| 17 | + * <p> |
| 18 | + * string convert(string s, int numRows); |
| 19 | + * 示例 1: |
| 20 | + * <p> |
| 21 | + * 输入: s = "LEETCODEISHIRING", numRows = 3 |
| 22 | + * 输出: "LCIRETOESIIGEDHN" |
| 23 | + * 示例 2: |
| 24 | + * <p> |
| 25 | + * 输入: s = "LEETCODEISHIRING", numRows = 4 |
| 26 | + * 输出: "LDREOEIIECIHNTSG" |
| 27 | + * 解释: |
| 28 | + * <p> |
| 29 | + * L D R |
| 30 | + * E O E I I |
| 31 | + * E C I H N |
| 32 | + * T S G |
| 33 | + * |
| 34 | + * @see <a href="https://leetcode-cn.com/problems/zigzag-conversion/">zigzag-conversion</a> |
| 35 | + */ |
| 36 | +public class _6_convert { |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + _6_convert convert = new _6_convert(); |
| 40 | + System.out.println(convert.convert("LEETCODEISHIRING", 4)); |
| 41 | + System.out.println(convert.convert("LE", 1)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * 执行用时 : 75 ms, 在ZigZag Conversion的Java提交中击败了34.97% 的用户 |
| 46 | + * 内存消耗 : 51.3 MB, 在ZigZag Conversion的Java提交中击败了18.60% 的用户 |
| 47 | + * <p> |
| 48 | + * 耗时在数组两次循环 |
| 49 | + * 优化->一次循环,不依赖数据 |
| 50 | + * |
| 51 | + * @param s |
| 52 | + * @param numRows |
| 53 | + * @return |
| 54 | + */ |
| 55 | + public String convert(String s, int numRows) { |
| 56 | + if (s.length() <= numRows || numRows == 1) { |
| 57 | + return s; |
| 58 | + } |
| 59 | + //构建存储数据 |
| 60 | + int l = numRows * 2 - 2; |
| 61 | + int l1 = s.length() / l; |
| 62 | + int l2; |
| 63 | + int ms = s.length() % l; |
| 64 | + if (ms == 0) |
| 65 | + l2 = 0; |
| 66 | + else if (ms < numRows) |
| 67 | + l2 = 1; |
| 68 | + else |
| 69 | + l2 = s.length() % l - numRows + 1; |
| 70 | + |
| 71 | + char[][] ss = new char[numRows][(l1 * (numRows - 1) + l2)]; |
| 72 | + char[] chars = s.toCharArray(); |
| 73 | + for (int i = 0; i < chars.length; i++) { |
| 74 | + int ml = i % l; |
| 75 | + int col, row; |
| 76 | + if (ml < numRows) { |
| 77 | + row = ml; |
| 78 | + } else { |
| 79 | + row = l - ml; |
| 80 | + } |
| 81 | + int dl = i / l; |
| 82 | + if (ml < numRows) { |
| 83 | + col = dl * (numRows - 1); |
| 84 | + } else { |
| 85 | + col = dl * (numRows - 1) + ml - numRows + 1; |
| 86 | + } |
| 87 | + ss[row][col] = chars[i]; |
| 88 | + } |
| 89 | + |
| 90 | + StringBuilder builder = new StringBuilder(); |
| 91 | + for (int i = 0; i < ss.length; i++) { |
| 92 | + for (int j = 0; j < ss[i].length; j++) { |
| 93 | + if (ss[i][j] == '\u0000') { |
| 94 | + continue; |
| 95 | + } |
| 96 | + builder.append(ss[i][j]); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return builder.toString(); |
| 101 | + } |
| 102 | +} |
0 commit comments