My solution in C++


  • 0
    P
    class Solution 
    {
    public:
    	string convert(string s, int numRows) 
    	{
    		if (numRows == 1) return s;
    		vector<string> vString(numRows);
    		int div = 2 * numRows - 2;
    		for (int i = 0, temp = 0; i < s.size(); ++i)
    		{
    			temp = i % div;
    			if (temp < numRows)
    				vString[temp] += s[i];
    			else
    				vString[2 * numRows - temp - 2] += s[i];
    		}
    		string ret;
    		for (const auto & item : vString) { ret += item; }
    		return ret;
    	}
    }

Log in to reply
 

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