C++ solution 16ms


  • 0
    Z
    string convert(string s, int numRows) {
    	string s_result = "";
    	int cur_loc = 0;
    	int s_size = s.size();
    	if (1 == numRows || numRows >= s_size){
    		return s;
    	}
    	int i = 1;
    	int j = 1;
    
    	for (; (cur_loc = 2 * (numRows - 1) * (j - 1)) < s_size; j++){
    		s_result += s[cur_loc];
    	}
    
    	i++;
    	for (; i < numRows; i++){
    		for (j = 1; (cur_loc = i - 1 + 2 * (numRows - 1) * (j - 1)) < s_size; j++){
    			s_result += s[cur_loc];
    			if ((cur_loc + 2 * numRows - 2 * i) < s_size)
    				s_result += s[cur_loc + 2 * numRows - 2 * i];
    		}
    	}
    
    	for (j = 1; (cur_loc = i - 1 + 2 * (numRows - 1) * (j - 1)) < s_size; j++){
    		s_result += s[cur_loc];
    	}
    	return s_result;
    }
    

    Just some location exchanges, it may not seem to be easy to understand.


Log in to reply
 

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