列表

详情


6. Z 字形变换

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

P   A   H   N
A P L S I I G
Y   I   R

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

请你实现这个将字符串进行指定行数变换的函数:

string convert(string s, int numRows);

 

示例 1:

输入:s = "PAYPALISHIRING", numRows = 3
输出:"PAHNAPLSIIGYIR"
示例 2:
输入:s = "PAYPALISHIRING", numRows = 4
输出:"PINALSIGYAHRPI"
解释:
P     I    N
A   L S  I G
Y A   H R
P     I

示例 3:

输入:s = "A", numRows = 1
输出:"A"

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: string convert(string s, int numRows) { } };

golang 解法, 执行用时: 16 ms, 内存消耗: 6.2 MB, 提交时间: 2020-11-11 11:37:54

func convert(s string, numRows int) string {
	if numRows == 1 {
		return s
	}
	length := len(s)
	if length < numRows {
		length = numRows
	}
	rows := make([]string, length)
	curRow, goingDown := 0, false
	for i, _ := range s {
		rows[curRow] += string(s[i])
		if curRow == 0 || curRow == numRows - 1 {
			goingDown = !goingDown
		}
		if goingDown {
			curRow += 1
		} else {
			curRow -= 1
		}
	}
	return strings.Join(rows, "")
}

cpp 解法, 执行用时: 16 ms, 内存消耗: 10.8 MB, 提交时间: 2020-11-11 11:36:27

class Solution {
public:
    string convert(string s, int numRows) {

        if (numRows == 1) return s;

        vector<string> rows(min(numRows, int(s.size())));
        int curRow = 0;
        bool goingDown = false;

        for (char c : s) {
            rows[curRow] += c;
            if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
            curRow += goingDown ? 1 : -1;
        }

        string ret;
        for (string row : rows) ret += row;
        return ret;
    }
};

golang 解法, 执行用时: 12 ms, 内存消耗: 7.1 MB, 提交时间: 2020-11-11 11:06:01

func convert(s string, numRows int) string {
	if numRows == 1 {
		return s
	}
	n, cycleLen := len(s), 2*numRows-2
	ans := ""
	for i := 0; i < numRows; i++ {
		for j := 0; j < n-i; j += cycleLen {
			ans += string(s[j+i])
			if i != 0 && i != numRows-1 && j+cycleLen-i < n {
				ans += string(s[j+cycleLen-i])
			}
		}
	}
	return ans
}

python3 解法, 执行用时: 68 ms, 内存消耗: 13.6 MB, 提交时间: 2020-11-11 10:50:07

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1:
            return s
        n = len(s)
        cycleLen = 2 * numRows - 2  # 一次循环长度
        ans = ''
        for i in range(0, numRows):
            for j in range(0, n-i, cycleLen):
                ans += s[j + i]   # 第一行和最后一行
                if i != 0 and i != numRows - 1 and j + cycleLen - i < n:
                    ans += s[j + cycleLen - i]   # 中间的行
        return ans
        

python3 解法, 执行用时: 80 ms, 内存消耗: 13.6 MB, 提交时间: 2020-11-11 10:48:43

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows == 1:
            return s
        n = len(s)
        cycleLen = 2 * numRows - 2  # 一次循环长度
        ans = ''
        for i in range(0, numRows):
            for j in range(0, n-i, cycleLen):
                ans += s[j + i]
                if i != 0 and i != numRows - 1 and j + cycleLen - i < n:
                    ans += s[j + cycleLen - i];
        return ans
        

上一题