Leetcode 6 ZigZag Conversion (z-type conversion)

Source: Internet
Author: User

Translation

字符串“PAYPALISHIRING”通过一个给定的行数写成如下这种Z型模式:P   A   H   NII GY   I   R然后一行一行的读取:“PAHNAPLSIIGYIR”写代码读入一个字符串并通过给定的行数做这个转换:string convert(string text, int nRows);调用convert("PAYPALISHIRING"3),应该返回"PAHNAPLSIIGYIR"。

Original

Thestring "Paypalishiring"  isWritteninchA zigzag pattern onAgiven  Number  ofRows like this: (Want toDisplay this patterninchA fixed font forBetter legibility) P A H NA p L S i i GY i RAnd Then ReadLine byLine"Pahnaplsiigyir"Write theCode thatwould take astring  andMake this conversiongivenA Number  ofRowsstringConvertstring text, int nRows); CONVERT ("Paypalishiring",3) shouldreturn "Pahnaplsiigyir".

If you still do not understand the meaning of the topic, see ...

 Public classsolution{ Public string Convert(stringSintNumRows) {if(NumRows = =1)returnS StringBuilder Strbuilder =NewStringBuilder ();intLengthofgroup =2* NumRows-2;//As shown, the length of each group is 4         for(introw =0; Row < NumRows; row++)//Follow the sequence of rows from line No. 0 to NumRows-1{if(Row = =0|| row = = NumRows-1)//Here is responsible for line No. 0 and NumRows-1{ for(intj = row; J < S.length;                J + = Lengthofgroup) {strbuilder.append (s[j]); }            }Else                   //Here is responsible for all rows between line No. 0 and NumRows-1{intCurrentRow = row;//Move right in the current line (see)                BOOLFlag =true;intChildLenOfGroup1 =2* (NumRows-1-row);//How to say it ... Each index of the middle row                intChildLenOfGroup2 = Lengthofgroup-childlenofgroup1; while(CurrentRow < S.length) {strbuilder.append (S[currentrow]);if(flag) CurrentRow + = ChildLenOfGroup1;ElseCurrentRow + = childLenOfGroup2;                flag =!flag; }            }        }returnStrbuilder.tostring (); }}

The code for C + + must be there:

Class Solution { Public:string Convert(stringSintNumRows) {if(numrows==1)returnSstringStr="";intlengthofgroup=2*numrows-2; for(introw=0; row<numrows;row++) {if(row==0|| row==numrows-1){ for(intCurrentrow=row;currentrow<s.length (); currentrow+=lengthofgroup) {Str+=s[currentrow]; }            }Else{intCurrentrow=row;BOOLflag=true;intchildlenofgroup1=2* (numrows-1-row);intChildlenofgroup2=lengthofgroup-childlenofgroup1; while(Currentrow<s.length ()) {Str+=s[currentrow];if(flag) Currentrow+=childlenofgroup1;Elsecurrentrow+=childlenofgroup2;                Flag=!flag; }            }        }returnStr }};

As for Java, of course there are ... But every time I just copied the code of C # and changed it.

 Public classSolution { PublicStringConvert(String S,intNumRows) {if(NumRows = =1)returnS StringBuilder Strbuilder =NewStringBuilder ();intLengthofgroup =2* NumRows-2; for(introw=0; Row < NumRows; row++) {if(Row = =0|| row = = NumRows-1){ for(intCurrentRow = row; CurrentRow < S.length ();                  CurrentRow + = Lengthofgroup) {strbuilder.append (S.charat (CurrentRow)); }              }Else{intCurrentRow = row; Boolean flag =true;intChildLenOfGroup1 =2* (NumRows-1-row);intChildLenOfGroup2 = Lengthofgroup-childlenofgroup1; while(CurrentRow <s.length ()) {Strbuilder.append (S.charat (CurrentRow));if(flag) CurrentRow + = ChildLenOfGroup1;ElseCurrentRow + = childLenOfGroup2;                  flag =!flag; }              }          }returnStrbuilder.tostring (); }}

Copyright NOTICE: This article is nomasp Couvant original article, without permission is prohibited reprint! Welcome to my blog: http://blog.csdn.net/nomasp

Leetcode 6 ZigZag Conversion (z-type conversion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.