My Java solution - 7ms 10 lines


  • 1
    L
    public String convert3(String s, int n) {   //7ms
            if (n == 1)
                return s;
            char res[] = new char[s.length()];
            for (int row = 0,i=0,groupCount = 2*n-2; row < n; row++) //一次加一行
                for (int index = row,secIndex = groupCount-row; index < s.length();index += groupCount,secIndex += groupCount){
                    res[i++] = s.charAt(index);
                    if (row >0 && row < n-1 && secIndex < s.length())
                        res[i++] = s.charAt(secIndex);
                }
            return new String(res);
        }

Log in to reply
 

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