My simple solution


  • 0
    V
    class Solution {
    public:
        string convert(string s, int numRows) {
            if( numRows >= s.size() || numRows == 1 )
                return s;
        
            int step;
            int mid;
            string result;
            int gap = 2 * ( numRows - 1 );
            for( int i = 0; i < numRows; i++ )
            {
                step = gap - i * 2;
            
                for( int j = i; j < s.size(); j += gap )
                {
                    result.push_back( s[ j ] );
    
                    mid = j + step;
                    if( step && step < gap && mid < s.size() )
                        result.push_back( s[ mid ] );
                }
            }
        
            return result;
        }
    };

Log in to reply
 

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