C++solution


  • 0
    H

    **class Solution{
    public:
    string convert(string s, int numRows) {

    	if (numRows == 1)
    	{
    		return s;
    	}
    
    	int size = s.size();
    	int row = numRows;
    
    	int n = size % (2 * numRows - 2);
    	if (n <= numRows)
    		n = 1;
    	else
    		n = 1 + n - numRows;
    
    	int col = size / (2 * numRows - 2)*(numRows - 1) + n;
    	vector<vector<char>> v(row,vector<char>(col,0));
    
    	int k = 0;
    	int yu = 0;
    	for (int j = 0; j<col; j++)
    	{
    
    		if ((yu = j % (numRows - 1)) == 0)
    		{
    			for (int i = 0; i<row && k<s.size(); i++)
    			{
    				v[i][j] = s[k++];
    			}
    		}
    		else
    		{
    			v[row - 1 - yu][j] = s[k++];
    			//v[yu][j] = s[k++];
    		}
    	}
    	k = 0;
    	string ret(s.size() + 1,0);
    	for (int i = 0; i<row; i++)
    	{
    		for (int j = 0; j<col; j++)
    		{
    			if (v[i][j] != 0)
    				ret[k++] = v[i][j];
    		}
    	}
    	return ret;
    
    }
    

    };**


Log in to reply
 

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