Solution by Tommy


  • 0
    T
    string convert(string s, int numRows) {
        string result = "";
        int length = s.length();
        int offset = 2 * numRows - 2;
        int startIndex1, startIndex2;
        if(s.empty()) return result;
        if(numRows == 1 || s.length() <= numRows) return s;
        for(int i = 0 ; i < numRows ; i++){
            startIndex1 = i;
    	    startIndex2 = i + offset - (i * 2);
    	    while(1){
                if(startIndex1 >= length) break;
                result += s[startIndex1];
    		    startIndex1 += offset;
    		    if(i > 0 && i < numRows - 1){
                    if(startIndex2 >= length) break;
                    result += s[startIndex2];
    		        startIndex2 += offset;
    	       }
    	    }
        }
        return result;
    }

Log in to reply
 

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