LeetCode-6 ZigZag Conversion

Source: Internet
Author: User

Two methods of solution

1. The string is first spelled into a diagram

1      PublicString Convert (String s,intnumrows) {2         intL=s.length ();3         if(l<=numrows)4             returns;5         if(numrows==1)6             returns;7         intcol=numrows-1;//number of columns per block8         intNumcols= (int) Math.ceil (l*1.0/(2*numrows-2)) *COL;9         Char[] cs=New Char[Numrows][numcols];Ten         intindexblock=0,index=0;//block order, number in block One         intr=0,c=0;//the row and column where A         // -         //System.out.println ("Number of columns is" +numcols); -         // the          for(inti=0;i<numrows;i++) -         { -              for(intj=0;j<numcols;j++) -             { +Cs[i][j]=0x7fff; -             } +         } A          for(inti=0;i<l;i++) at         { -Indexblock= (int) Math.floor (i*1.0/(2*numrows-2)); -Index= (int) (i% (2*numrows-2)); -             if(index<numrows)//Vertical Column -             { -c=indexblock*COL; inR=index; -cs[r][c]=S.charat (i); to                 //System.out.println ("vertical column" +i+ "th char" +s.charat (i) + "row=" +r+ "col=" +c); +             } -             Else //Oblique the             { *C=indexblock*col+index+1-NumRows; $r=numrows-(index-numrows)-2;Panax Notoginsengcs[r][c]=S.charat (i); -                 //System.out.println ("Oblique column" +i+ "th char" +s.charat (i) + "row=" +r+ "col=" +c); the             } +         } A         //Output theStringBuilder builder=NewStringBuilder (); +          for(inti=0;i<numrows;i++) -         { $              for(intj=0;j<numcols;j++) $             { -                 if(CS[I][J]!=0X7FFF) -                 { the builder.append (Cs[i][j]); -                 }Wuyi             } the         } -          Wu         returnbuilder.tostring (); -}

2, dynamic add

1     PublicString Convert (String s,intnumrows) {2         if(numrows==1)returns;3         if(S.length () <numrows)returns;4String[] Strs=NewString[numrows];5         //Initial6          for(inti=0;i<numrows;i++)7         {8Strs[i]= "";9         }Ten         intIndex=0,gap=numrows-2,rowindex=0; One          while(index<s.length ()) A         { -              for(Rowindex=0;rowindex<numrows && index<s.length (); rowindex++)  -             { thestrs[rowindex]+= "" +s.charat (index++); -             } -              -              for(Rowindex=gap;rowindex<=gap && rowindex>0 && index<s.length (); rowindex--) +             { -strs[rowindex]+= "" +s.charat (index++); +             } A              at         } -         //Output -StringBuilder builder=NewStringBuilder (); -          for(inti=0;i<numrows;i++) -         { - builder.append (Strs[i]); in         } -         returnbuilder.tostring (); to}

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.