go language implention


  • 0
    M
    func convert(s string, numRows int) string {
    	if numRows == 1 {
    		return s
    	}
        var buffer bytes.Buffer
    	//result := make([]byte, 0)
    	down := true
    	i,j := 0,0
    	table := make(map[string]byte)
    	for _, value := range []byte(s) {
    		table[fmt.Sprintf("%d-%d", i, j)] = value
    		if i <numRows -1 && i > 0 {
    			if down {
    				i = i + 1
    			} else {
    				i = i - 1
    				j = j + 1
    			}
    		} else if i == 0 {
    			down = true
    			i = i + 1
    		} else if i == numRows -1 {
    			down = false
    			i = i - 1
    			j = j + 1
    		}
    	}
    	for k := 0; k < numRows ; k++ {
    		for g := 0; g <= j ; g++ {
    			if value, ok := table[fmt.Sprintf("%d-%d", k, g)]; ok {
                    buffer.WriteString(string(value))
    			}
    		}
    	}
        return buffer.String()
    }
    

Log in to reply
 

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