@@ -0,0 +1,2025 @@
+package main.algorithm.leetcode;%0A%0Aimport org.junit.Test;%0A%0A/**%0A * Title : main.algorithm.leetcode %3Cbr%3E%0A * Description :%0A * %3Cp%3E Runtime: 24 ms%0A * %3Cp%3E Memory Usage: 40.8 MB%0A * @author chile%0A * @version 1.0%0A * @date 2019/3/18 16:31%0A */%0Apublic class ZigZagConversion %7B%0A%0A    class Solution %7B%0A        /**%0A         * not good enough%0A         * @param s%0A         * @param numRows%0A         * @return%0A         */%0A        public String convert(String s, int numRows) %7B%0A            int r = numRows, c = 0, l = s.length();%0A%0A            if (r == 1) %7B%0A                return s;%0A            %7D%0A%0A            int numGroups = l / (r + r - 2);%0A            int numGrouped = numGroups * (r + r - 2);%0A            c = numGroups * (r - 1) + (l - numGrouped %3E r ? l - numGrouped - r + 1 : 1);%0A%0A            int i = 0, j = 0, x = 1, y = 0;%0A            char%5B%5D words = s.toCharArray();%0A            char%5B%5D%5B%5D matrix = new char%5Br%5D%5Bc%5D;%0A            for (char w : words) %7B%0A                matrix%5Bi%5D%5Bj%5D = w;%0A                if (i == 0) %7B%0A                    x = 1;%0A                    y = 0;%0A                %7D%0A                if (i == r - 1) %7B%0A                    x = -1;%0A                    y = 1;%0A                %7D%0A                i += x;%0A                j += y;%0A            %7D%0A%0A            char ch = '%5Cu0000';%0A            StringBuilder sb = new StringBuilder();%0A            for (i = 0; i %3C r; i++) %7B%0A                for (j = 0; j %3C c; j++) %7B%0A                    if (matrix%5Bi%5D%5Bj%5D != ch) %7B%0A                        sb.append(matrix%5Bi%5D%5Bj%5D);%0A                    %7D%0A                %7D%0A            %7D%0A%0A            // System.out.println(sb.toString());%0A%0A            return sb.toString();%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void test1() %7B%0A        assert new Solution().convert(%22PAYPALISHIRING%22, 3).equals(%22PAHNAPLSIIGYIR%22);%0A    %7D%0A%0A    @Test%0A    public void test2() %7B%0A        assert new Solution().convert(%22PAYPALISHIRING%22, 4).equals(%22PINALSIGYAHRPI%22);%0A    %7D%0A%0A    @Test%0A    public void test3() %7B%0A        assert new Solution().convert(%22ABCDE%22, 4).equals(%22ABCED%22);%0A    %7D%0A%0A%7D%0A

zig zag