將乙個給定字串根據給定的行數,以從上往下、從左到右進行 z 字形排列。
比如輸入字串為 "leetcodeishiring" 行數為 3 時,排列如下:
請你實現這個將字串進行指定行數變換的函式:
string convert(string s, int numrows);示例1:
輸入: s = "leetcodeishiring", numrows = 3示例2:輸出: "lciretoesiigedhn"
輸入: s = "leetcodeishiring", numrows = 4輸出: "ldreoeiiecihntsg"
解釋:
根據題意描述,按照從上到下,從左到右逐個讀取z字形圖案的順序訪問字串。
思路分析:
首先訪問第0行中的所有字串,接著訪問第1行,然後第2行.......對於所有整數n,
- 行 0 中的字元索引位置:n (2 numrows - 2);- 行 i 中的字元索引位置:n (2 numrows - 2) - i 和 (n 1) (2 numrows - 2) - i;
- 行 numrows - 1 中的字元索引位置:n (2 numrows - 2) numrows - 1;
- 注意:下標n不能超過*len(s)-1*;
**示例:
複雜度分析:public string convert(string s, int numrows) }}
return sb.tostring();
}
- 時間複雜度:*o(n)*, n==len(s)。每個索引都被訪問一次。- 空間複雜度:*o(n)*
leetcode Z字形變換
將乙個給定字串根據給定的行數,以從上往下 從左到右進行 z 字形排列。比如輸入字串為 leetcodeishiring 行數為 3 時,排列如下 l c i r e t o e s i i g e d h n 之後,你的輸出需要從左往右逐行讀取,產生出乙個新的字串,比如 lciretoesiiged...
leetcode Z字形變換
將乙個給定字串根據給定的行數,以從上往下 從左到右進行 z 字形排列。比如輸入字串為 leetcodeishiring 行數為 3 時,排列如下 之後,你的輸出需要從左往右逐行讀取,產生出乙個新的字串,比如 lciretoesiigedhn 請你實現這個將字串進行指定行數變換的函式 string c...
LeetCode Z字形變換
繼續刷題 題目 z字形變換 將乙個給定字串根據給定的行數,以從上往下,從左往右進行z字形排列。比如輸入字串為 leetcodeishiring 行數為 3 時,排列如下 l c i r e t o e s i i g e d h n之後,你的輸出需要從左往右逐行讀取,產生乙個新的字串,比如 lcir...