Ugly Code i guess but accepted...used matrix


  • 0
    B
    class Solution {
        public String convert(String s, int numRows) {
    
            char[][] matrix = new char[numRows][(s.length()/2)+1];
            StringBuilder result = new StringBuilder();
            int v=-1;
            int h=0;
            int k=0; // string finish
            if(numRows >= s.length())
                return s;
            if(numRows == 1)
                return s;
            if(s.length()==0)
                return "";
            while(true)
            {
                ++v;
                while(v<numRows)
                {
    
                    matrix[v][h]=s.charAt(k);
                    ++k;
                    ++v;
                    if(k>=s.length())
                    {
                        for(int i=0;i<numRows;i++)
                        {
                            for(int j=0;j<(s.length()/2)+1;j++)
                            {
                                if(matrix[i][j] != '\u0000')
                                    result.append(matrix[i][j]);
                            }
                        }
                        return result.toString();
                    }
    
                }
            v=v-2;
                while(v>=0)
                {
    
                    ++h;
                    matrix[v][h]=s.charAt(k);
                    ++k;
                    if(k>=s.length())
                    {
    
                        for(int i=0;i<numRows;i++)
                        {
                            for(int j=0;j<(s.length()/2)+1;j++)
                            {
                                if(matrix[i][j] != '\u0000')
                                    result.append(matrix[i][j]);
                            }
                        }
                        return result.toString();
                    }
                    --v;
                }
                ++v;
               // ++h;
                //v=0;
    
    
            }
    
            // return result.toString();
    
    
        }
    
    }
    

Log in to reply
 

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