Easy C++ solution beat 99.93%


  • 0
    C
    class Solution {
    public:
        string convert(string s, int numRows) {
    	int arraySize = 2 * (numRows-1);
    	if (arraySize <= 0) {
    		return s;
    	}
    	string result;
    	for (int i = 0; i < numRows; i++) {
    		for (int j = 0;; j++) {
    			int index1 = j*arraySize + i;
    			if (index1 < s.size()) {
    				result += s[index1];
    				if (i == 0 || i == numRows - 1) {
    					continue;
    				}
    				int index2 = index1 + arraySize - (i << 1);// (j + 1)*arraySize - i;
    				if (index2 < s.size()) {
    					result+=s[index2];
    				}
    				else {
    					break;
    				}
    			}
    			else
    			{
    				break;
    			}
    		}
    	}
    	return result;
        }
    };
    

Log in to reply
 

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