Golang solution in 12ms


  • 0
    L
    func convert(s string, numRows int) string {
    	if numRows <= 1 {
    		return s
    	}
    	len, cycle, n :=  len(s), 2 * numRows - 2, 0
    	result := make([]byte, len)
    	for i := 0; i < numRows; i++ {
    		for j1 := i; j1 < len; j1 = cycle + j1 {
    			n += copy(result[n:], string(s[j1]))
    			j2 := j1 + cycle - 2*i
    			if i != 0 && i != numRows-1 && j2 < len {
    				n += copy(result[n:], string(s[j2]))
    			}
    		}
    	}
    	return string(result)
    }
    

Log in to reply
 

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