My C++Solution (35ms)


  • 2
    V
    	string convert(string s, int nRows) {
    	string ans[nRows];
    	int j = 0;
    	int pos = 0;
    	if(nRows <= 1)
    		return s;
    	for(int i = 0; i < s.length(); ++i)
    	{ 
    		int tmp = pos / (nRows - 1);
    		if(tmp % 2 == 0)
    			ans[j++] += s[i];
    		else
    			ans[j--] += s[i];
    		pos++;
    	}
    	string result;
    	for(int i = 0; i < nRows; ++i)
    		result += ans[i];
    	return result;
    	    
    }

  • 1
    3

    Actually pos equals i, so u don't need pos.


  • 0
    V

    oh! I discovered it. Thanks!


Log in to reply
 

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