my way


  • 0
    S

    public static String zigzig_test2(String s, int numRows) {

        // 行数: numRows 
        int len = s.length();
        if (len <= 1 || numRows == 1) return s;
        StringBuffer[] sb = new StringBuffer[numRows];
        for (int i = 0; i < numRows; i++) {
            sb[i] = new StringBuffer();
        }
        boolean flag = true;
        int index = 0;
        int row = 1;
    
        while (index < s.length()) {
    
            sb[row-1].append(s.charAt(index));
    
            if(flag == true) {
                row++;
                if(row == numRows)
                    flag = false;
            }
            else {
                row--;
                if(row == 1)
                    flag = true;
            }
    
            index++;
        }
    
        for (int i = 1; i < numRows; i++) {
            sb[0].append(sb[i]);
        }
    
        return  sb[0].toString();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.