Leetcode 6--ZigZag Conversion

Source: Internet
Author: User

The string is "PAYPALISHIRING" written with 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   NA p L S i i GY   i   R

And then read on line:"PAHNAPLSIIGYIR"

Write the code that would take a string and make this conversion given a number of rows:

String convert (string text, int nRows);

convert("PAYPALISHIRING", 3)should return "PAHNAPLSIIGYIR" .

Oneself come up not to understand what is zigzag, also thought is more in the point, the result is not right, make quite complex, right vote paste code, when a lesson:

1  Public classzigzagconversion {2      Public StaticString Convert (String s,intnumrows) {3         if(s = =NULL|| S.length () = = 0)4             returns;5         if(NumRows < 2)6             returns;7         intGap = NumRows-2;8String res = "";9 Ten          for(inti = 0; i < numrows; i++) { One             if(i = = 0) { A                  for(intj = i; J < S.length (); J + = (NumRows * 2-2)) -Res + =S.charat (j); -}Else if(i = = NumRows-1) { the                  for(intj = i; J < S.length (); J + = (NumRows * 2-2)) -Res + =S.charat (j); -}Else { -                  for(intj = i; J < S.length (); J + = (NumRows * 2-2)) { +Res + =S.charat (j); -                     if(j + numrows * 2-3-i <s.length ()) +Res + = S.charat (j + numrows * 2-3-i); A                 } at             } -         } -  -         returnRes; -     } -  in      Public Static voidMain (string[] args) { -String s = "ABCDE"; toString res = CONVERT (S, 4); + System.out.println (res); -     } the  *}

Problem code, boundary problem not solved well

1  Public classSolution {2      PublicString Convert (String s,intnRows) {3         if(s = =NULL|| S.length () <= NRows | | NRows <= 1)returns;4StringBuffer SB =NewStringBuffer ();5         //The first row6          for(inti = 0; I < s.length (); i + = (nRows-1) * 2) {7 Sb.append (S.charat (i));8         }9         Ten          for(inti = 1; i < nRows-1; i++) { One              for(intj = i; J < S.length (); j+= (nRows-1) * 2) { A Sb.append (S.charat (j)); -                 if(j + (Nrows-i-1) * 2 <s.length ()) { -Sb.append (S.charat (j + (Nrows-i-1) * 2)); the                 } -             } -         } -         //The last row +          for(inti = nRows-1; I < s.length (); i + = (nRows-1) * 2) { - Sb.append (S.charat (i)); +         } A         returnsb.tostring (); at     } -}

Baidu later, see others code is quite concise, Fang know that they did not understand the topic.

Zigzag: That is, the cyclic diagonal structure (

0 8 16
1 7 9 15 17
2 6 10 14 18
3 5 11 13 19
4 12 20

Constructs n strings, loops each to add the corresponding characters, and finally merges all the characters;

I re-wrote according to the idea:

1  Public classzigzagconversion {2      Public StaticString Convert (String s,intnumrows) {3         if(s = =NULL|| S.length () = = 0)4             returns;5         if(NumRows < 2)6             returns;7string[] Res =NewString[numrows];8String result = "";9         Ten         inti = 0, J; One  A          while(I <s.length ()) { -              for(j = 0; J < numrows && I < S.length (); j + +) { -RES[J] + =S.charat (i); thei++; -             } -              for(j = numRows-2; J > 0 && I < s.length (); j--) { -RES[J] + =S.charat (i); +i++; -             } +         } A          for(intk = 0; K < numrows;k++) atResult + =Res[k]; -         returnresult; -     } -  -      Public Static voidMain (string[] args) { -String s = "paypalishiring"; inString res = CONVERT (S, 3); - System.out.println (res); to     } +}

Just not once the string array is initialized to null by default, how the overwrite has not been resolved, the original text in C + + code is as follows:

1 stringConvertstringSintnRows) {2     if(NRows = =1)returns;3     stringRes[nrows];4     inti =0, J, Gap = nrows-2;5      while(I <s.size ()) {6          for(j =0; I < S.size () && J < NRows; ++J) res[j] + = s[i++];7          for(j = gap; I < S.size () && J >0; --J) res[j] + = s[i++];8     }9     stringstr ="";Ten      for(i =0; i < nRows; ++i) Onestr + =Res[i]; A     returnstr; -}

Find some of their own problems: the first is not very good to think about the problem, or that their ideas are always some deviation, followed by the boundary problem, always make small mistakes.

Leetcode 6--ZigZag 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.