Golang Leetcode 6. ZigZag Conversion.go

版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89067106

思路

关键是要算出循环的长度:
为 row + row -2

code

func convert(s string, numRows int) string {
	if numRows == 1 {
		return s
	}

	item_len := 2*numRows - 2 //循环的长度
	res := make([][]string, numRows, numRows)

	for index, v := range s {

		mod := index % item_len

		if mod < numRows {
			res[mod] = append(res[mod], string(v))
		} else {
			i := numRows - (mod - numRows) - 2
			res[i] = append(res[i], string(v))
		}
	}

	var str string

	for _, arr := range res {
		for _, v := range arr {
			str += v
		}
	}

	return str
}

更多内容请移步我的repo:https://github.com/anakin/golang-leetcode

猜你喜欢

转载自blog.csdn.net/anakinsun/article/details/89067106