这道题是要找出每个字符跳转的规律,还是比较好找的 关键是找到zigGap和zagGap

public class Solution {
    public String convert(String s, int numRows) {
        
        if (s.equals("")) {
            return s;
        }
        
        if (numRows == 0) {
            return "";
        }
        
        if (numRows == 1) {
            return s;
        }
        
        int maxGap = 2 * numRows - 2;
        char[] resultChars = new char[s.length()];
        int zigGap = maxGap;
        int zagGap = 0;
        int start = 0;
        int q = 0;
        while (zigGap >= 0) {
            int p = start;
            
            if (p >= s.length())
                break;
            
            resultChars[q] = s.charAt(p);
            q++;
            while (p < s.length()) {
                p = p + zigGap;
                
                if (p >= s.length())
                    break;
                
                if (zigGap > 0) {
                    resultChars[q] = s.charAt(p);
                    q++;
                }
                p = p + zagGap;
                if (p >= s.length())
                    break;
                if (zagGap > 0) {
                    resultChars[q] = s.charAt(p);
                    q++;
                }
            }
            zigGap = zigGap - 2;
            zagGap = zagGap + 2;
            start++;
        }
        String resultString = new String(resultChars);
        return resultString;
    }
}

chenatu
106 声望12 粉丝