Requirement: The given string is output as "inverted n", specifying the number of rows to be output
function string Convert (string s, int numrows)
For example, enter "ABCDEFGHIJKLNMOPQRSTUVWXYZ", output to 3 lines;
A e i n q u y
Bdfhjlmprtvxz
C G K O s W
Here is an example of a 5 row
String s = "abcdefghijklnmopqrstuvwxyzabcdefghijklnmopqrstuvwxyzabcdefghijklnmopqrstuvwxyz";
A___i___q___y___g___o___w___e___n___u
B__hj__pr__xz__fh__mp__vx__df__lm__tv
C_g_k_o_s_w_a_e_i_n_q_u_y_c_g_k_o_s_w
Df__lm__tv__bd__jl__rt__zb__hj__pr__xz
E___n___u___c___k___s___a___i___q___y
Easy to observe, with an underscore instead of a space, you can see there is no space at the end of the line;
Observation Examples:
1. Starting from 0, the No. 0 column of line No. 0 is "a"; the 4th row No. 0 column is "e"; the letter in the slash is called a slash bit.
2. The interval between the complete columns is 3, or 5-2, and for the example of 3 rows, the interval is an example of 1=3-2;2 line, with an interval of 0=2-2; the interval is numRows-2;
3. The first line and the tail line do not have the slash position; Observe the number and learn that the interval between a and I 2*numrows-2; make zigspace=2*numrows-2
4. For the number of spaces, there are 3 spaces between the No. 0 line of letters, 2 spaces on the left of line 1th Slash, 0 on the right;
Line 2nd diagonal 1 spaces to the left, 1 on the right, 0 spaces to the left of line 3rd Slash, 2 on the right
The position of the slash character here is: 2*numrows-2 + j-2*i (where I is the number of rows and J is the first character of the line)
5. After the last column no more spaces are added and the cursor can be crossed to determine
Convertoneline in code reads the result from left to right into a line
1 /**2 * @authorRust Fisher3 * @version1.04 */5 Public classZigZag {6 /**7 * @params8 * @paramNumRows9 * @returnThe string that already sortTen */ One Public StaticString Convert (String s,intnumrows) { A if(numrows <= 1 | | s.length () < NumRows | | s.length () < 3) { - returns; - } theString strresult = ""; - intZigspace = 2*numrows-2; - intZig = NumRows-2; - for(inti = 0; i < numrows; i++) { + for(intj = i; J < S.length (); j+=zigspace) { -strresult = Strresult +S.charat (j); + if(I! = 0 && I! = numRows-1 && (zigspace + j-2*i) <s.length ()) { A for(intInner = 0; Inner < zig-i; inner++) { atStrresult + = ""; - } -strresult = strresult + s.charat (zigspace + j-2*i); - if((2*zigspace + j-2*i) <= s.length ()/*true*/) {//control the final word of string - for(intInner = 0; Inner < i-1; inner++) { -Strresult + = ""; in } - } to}Else { + if(J+zigspace < S.length ()) {//control the final word of per line - for(intoutline = 0; Outline < Zig; outline++) { theStrresult + = ""; * } $ }Panax Notoginseng } - } the if(I < NumRows-1) { +Strresult + = "\ n"; A } the } + returnstrresult; - } $ /** $ * @params - * @paramNumRows - * @returnOne line String the */ - Public StaticString Convertoneline (String s,intnumrows) {Wuyi if(numrows <= 1 | | s.length () < NumRows | | s.length () < 3) { the returns; - } WuString strresult = ""; - intZigspace = 2*numrows-2; About for(inti = 0; i < numrows; i++) { $ for(intj = i; J < S.length (); j+=zigspace) { -strresult = Strresult +S.charat (j); - if(I! = 0 && I! = numRows-1 && (zigspace + j-2*i) <s.length ()) { -strresult = strresult + s.charat (zigspace + j-2*i); A } + } the } - returnstrresult; $ } the Public Static voidMain (String args[]) { theString s = "abcdefghijklnmopqrstuvwxyzabcdefghijklnmopqrstuvwxyzabcdefghijklnmopqrstuvwxyz"; theString ss = "ABCDEFGHIJKLNMOPQRSTUVWXYZ"; theSYSTEM.OUT.PRINTLN (CONVERT (ss,3)); -System.out.println (Convertoneline (ss,3)); in System.out.println (); theSYSTEM.OUT.PRINTLN (CONVERT (s,5)); theSystem.out.println (Convertoneline (s,5)); About } the}
Output:
A e i n q u y
Bdfhjlmprtvxz
C G K O s W
Aeinquybdfhjlmprtvxzcgkosw
A i q y g o w e n u
B HJ PR XZ FH MP VX DF LM TV
C G K O s w A e i n q u y c G K o s W
DF LM TV BD JL RT ZB HJ PR XZ
e n u c k s a i q y
Aiqygowenubhjprxzfhmpvxdflmtvcgkoswaeinquycgkoswdflmtvbdjlrtzbhjprxzenucksaiqy
But it has to be explained that the above method is too slow. Find another method on the Internet, the Java code is as follows:
PublicString Convert (String s,intnumrows) { if(s = =NULL|| NumRows < 1)return NULL; if(NumRows = = 1)returns; Char[] ss =S.tochararray (); Stringbuilder[] Strings=NewStringbuilder[numrows]; for(inti = 0; i < strings.length; i++) {Strings[i]=NewStringBuilder (); } intZignum = 2 * numRows-2; for(inti = 0; I < s.length (); i++) { intMoD = i%Zignum; if(MoD >=numrows) {strings[2*numrows-mod-2].append (Ss[i]); } Else{strings[mod].append (ss[i]); } } for(inti = 1; i < strings.length; i++) {strings[0].append (strings[i].tostring ()); } returnStrings[0].tostring (); }
Using the StringBuilder class to construct a string
ZigZag-Zigzag string