這篇文章將為大家詳細(xì)講解有關(guān)leetcode中如何解決ZigZag Conversion問題,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供譙城網(wǎng)站建設(shè)、譙城做網(wǎng)站、譙城網(wǎng)站設(shè)計(jì)、譙城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、譙城企業(yè)網(wǎng)站模板建站服務(wù),十多年譙城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N A P L S I I G Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
題意:
鋸齒形(Z字形)的變換。變換形式如下所示:
A)行數(shù)為兩行的情形: 0 2 4 1 3 6 B)行數(shù)為三行的情形: 0 4 8 1 3 5 7 9 2 6 10 C)行數(shù)為四行的情形: 0 6 12 1 5 7 11 13 2 4 8 10 14 3 9 15 D)行數(shù)為七行的情形: 第一行:0 12 24 第二行:1 11 13 23 25 第三行:2 10 14 22 26 第四行:3 9 15 21 27 第五行:4 8 16 20 28 第六行:5 7 17 19 29 第七行:6 18 30
觀察上面情形可知:
1)第一行和最后一行兩個(gè)元素之間的距離是:(行數(shù) - 1)* 2
2)中間幾行中第一個(gè)元素與第二個(gè)元素和第二個(gè)元素與第三個(gè)元素間的距離和是:(行數(shù) - 1)* 2.
3)中間幾行中第一個(gè)元素和第二個(gè)元素間的距離與第幾行的關(guān)系:(行數(shù)-第幾行)* 2.
4)中間幾行中第二個(gè)元素和第三個(gè)元素間的距離與第幾行的關(guān)系:(第幾行 - 1) * 2.
故有g(shù)etIndex()函數(shù):
1)如果是每行的第一個(gè)元素,直接返回行數(shù)。
2)如果是第一行或者最后一行的除首元素外的下標(biāo)獲取:前一個(gè)元素的下標(biāo) + (行數(shù) - 1)* 2
3)中間幾行的偶數(shù)列的下標(biāo)獲取:前一元素的下標(biāo) + (行數(shù)-第幾行)* 2
4)中間幾行的奇數(shù)列的下標(biāo)獲取(除首元素):前一元素下標(biāo) + (第幾行 - 1) * 2
注:
程序中行數(shù)由于是從零開始的的,所以程序中會(huì)有相差一的出入。
int
getIndex(int index, int cnt, int numRows, int flag)
{
if ( flag == 0 )
{
return cnt;
}
else if ( cnt == 0 || cnt == numRows - 1 )
{
index = index + ( numRows - 1) * 2;
}
else if ( flag % 2 != 0 )
{
index = index + (numRows - 1 - cnt) * 2;
}
else if ( flag % 2 == 0 )
{
index = index + cnt * 2;
}
return index;
}
char* convert(char* s, int numRows)
{
if ( *s == '\0' || numRows == 1 )
{
return s;
}
int len = strlen(s);
if ( len <= numRows )
{
return s;
}
int add = 0;
char rest[len + 1];
int cnt = 0;
for ( cnt = 0; cnt < numRows; cnt++ )
{
int index = 0;
int flag = 0;
index = getIndex(index, cnt, numRows, flag);
while ( index < len )
{
rest[add] = *(s + index);
add += 1;
flag += 1;
index = getIndex(index, cnt, numRows, flag);
}
}
rest[len] = '\0';
sprintf(s, "%s", rest);
return s;
}逐行讀取每個(gè)元素,存入rest結(jié)果集中。最后寫會(huì)s,防止出現(xiàn)局部數(shù)組返回。
關(guān)于“l(fā)eetcode中如何解決ZigZag Conversion問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁題目:leetcode中如何解決ZigZagConversion問題
文章出自:http://chinadenli.net/article42/iigjhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、品牌網(wǎng)站制作、全網(wǎng)營銷推廣、面包屑導(dǎo)航、App開發(fā)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)