將乙個給定字串根據給定的行數,以從上往下、從左到右進行 z 字形排列。
輸入: s = 「leetcodeishiring」, numrows = 4
輸出: 「ldreoeiiecihntsg」
**+注釋思路:l d r
e o e i i
e c i h n
t s g
最大迴圈執行的次數就是行數列數,n代表字串中字元的個數class solution
}//有了列數和行數,總個數=列*行
int total = numrows * columnnum;
char result = new char[total];
//開始插入字元
//插入的當前列
int curcolumn = 0;
int index = 0;
char chars = s.tochararray();
while (index < chars.length)
//豎列填完了,開始往後curcolumn++,n-2個斜列,每個斜列1個
curcolumn++;
//每迴圈一次填乙個斜列,curcolumn++
for (int i = slash; i > 0 && index < chars.length; i--)
}stringbuilder sb=new stringbuilder();
for (char c:result)
}return sb.tostring();
}}
行數為row,列數大致等於n/(2row-2)(row-1)即n/2,總次數就是n*row/2,當n足夠大時忽略係數,時間複雜度為o(n)
Z字形變換
題目 將字串 paypalishiring 以z字形排列成給定的行數 p a h n a p l s i i g y i r 之後從左往右,逐行讀取字元 pahnaplsiigyir 思路 通過從左向右迭代字串,我們可以輕鬆地確定字元位於 z 字形圖案中的哪一行。演算法 我們可以使用 min num...
Z 字形變換
將乙個給定字串根據給定的行數,以從上往下 從左到右進行 z 字形排列。比如輸入字串為 leetcodeishiring 行數為 3 時,排列如下 l c i r e t o e s i i g e d h n之後,你的輸出需要從左往右逐行讀取,產生出乙個新的字串,比如 lciretoesiigedh...
Z 字形變換
將乙個給定字串根據給定的行數,以從上往下 從左到右進行 z 字形排列。比如輸入字串為 leetcodeishiring 行數為 3 時,排列如下 l c i r e t o e s i i g e d h n之後,你的輸出需要從左往右逐行讀取,產生出乙個新的字串,比如 lciretoesiigedh...