问题描述
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"
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3)
should return "PAHNAPLSIIGYIR"
.
算法
代码:
1 /* 2 * 3 * 0A 8I 16Q 24Y 4 * 1B 7H 9J 15P17R 23X25Z 5 * 2C 6G 10K 14O 18S 22W 6 * 3D5F 11L13N 19T21V 7 * 4E 12M 20U 8 */ 9 public class ZigZagConversion { 10 public String convert(String s,int numRows){ 11 if(s.length()<=numRows||numRows==1) 12 return s; 13 StringBuilder []res=new StringBuilder[numRows]; 14 for(int i=0;i<numRows;i++) 15 res[i]=new StringBuilder(); 16 int loop=2*numRows-2; 17 int mid=numRows-1; 18 for(int i=0;i<s.length();i++){ 19 res[mid-Math.abs(i%loop-mid)].append(s.charAt(i)); 20 } 21 for(int i=1;i<numRows;i++) 22 res[0].append(res[i]); 23 return res[0].toString(); 24 25 } 26 27 }
注意事项:
1.对字符串的每个字符操作可以用toCharArray()得到一个char数组,也可以转换为StringBuffer对象或者StringBuilder对象,区别如下:
StringBuffer类
1 /** 2 * A thread-safe, mutable sequence of characters. 3 * A string buffer is like a {@link String}, but can be modified. At any 4 * point in time it contains some particular sequence of characters, but 5 * the length and content of the sequence can be changed through certain 6 * method calls. 7 * <p> 8 * String buffers are safe for use by multiple threads. The methods 9 * are synchronized where necessary so that all the operations on any 10 * particular instance behave as if they occur in some serial order 11 * that is consistent with the order of the method calls made by each of 12 * the individual threads involved. 13 * <p> 14 * The principal operations on a <code>StringBuffer</code> are the 15 * <code>append</code> and <code>insert</code> methods, which are 16 * overloaded so as to accept data of any type. Each effectively 17 * converts a given datum to a string and then appends or inserts the 18 * characters of that string to the string buffer. The 19 * <code>append</code> method always adds these characters at the end 20 * of the buffer; the <code>insert</code> method adds the characters at 21 * a specified point. 22 * <p> 23 * For example, if <code>z</code> refers to a string buffer object 24 * whose current contents are "<code>start</code>", then 25 * the method call <code>z.append("le")</code> would cause the string 26 * buffer to contain "<code>startle</code>", whereas 27 * <code>z.insert(4, "le")</code> would alter the string buffer to 28 * contain "<code>starlet</code>". 29 * <p> 30 * In general, if sb refers to an instance of a <code>StringBuffer</code>, 31 * then <code>sb.append(x)</code> has the same effect as 32 * <code>sb.insert(sb.length(), x)</code>. 33 * <p> 34 * Whenever an operation occurs involving a source sequence (such as 35 * appending or inserting from a source sequence) this class synchronizes 36 * only on the string buffer performing the operation, not on the source. 37 * <p> 38 * Every string buffer has a capacity. As long as the length of the 39 * character sequence contained in the string buffer does not exceed 40 * the capacity, it is not necessary to allocate a new internal 41 * buffer array. If the internal buffer overflows, it is 42 * automatically made larger. 43 * 44 * As of release JDK 5, this class has been supplemented with an equivalent 45 * class designed for use by a single thread, {@link StringBuilder}. The 46 * <tt>StringBuilder</tt> class should generally be used in preference to 47 * this one, as it supports all of the same operations but it is faster, as 48 * it performs no synchronization. 49 * 50 * @author Arthur van Hoff 51 * @version %I%, %G% 52 * @see java.lang.StringBuilder 53 * @see java.lang.String 54 * @since JDK1.0 55 */
StringBuilder类
1 /** 2 * A mutable sequence of characters. This class provides an API compatible 3 * with <code>StringBuffer</code>, but with no guarantee of synchronization. 4 * This class is designed for use as a drop-in replacement for 5 * <code>StringBuffer</code> in places where the string buffer was being 6 * used by a single thread (as is generally the case). Where possible, 7 * it is recommended that this class be used in preference to 8 * <code>StringBuffer</code> as it will be faster under most implementations. 9 * 10 * <p>The principal operations on a <code>StringBuilder</code> are the 11 * <code>append</code> and <code>insert</code> methods, which are 12 * overloaded so as to accept data of any type. Each effectively 13 * converts a given datum to a string and then appends or inserts the 14 * characters of that string to the string builder. The 15 * <code>append</code> method always adds these characters at the end 16 * of the builder; the <code>insert</code> method adds the characters at 17 * a specified point. 18 * <p> 19 * For example, if <code>z</code> refers to a string builder object 20 * whose current contents are "<code>start</code>", then 21 * the method call <code>z.append("le")</code> would cause the string 22 * builder to contain "<code>startle</code>", whereas 23 * <code>z.insert(4, "le")</code> would alter the string builder to 24 * contain "<code>starlet</code>". 25 * <p> 26 * In general, if sb refers to an instance of a <code>StringBuilder</code>, 27 * then <code>sb.append(x)</code> has the same effect as 28 * <code>sb.insert(sb.length(), x)</code>. 29 * 30 * Every string builder has a capacity. As long as the length of the 31 * character sequence contained in the string builder does not exceed 32 * the capacity, it is not necessary to allocate a new internal 33 * buffer. If the internal buffer overflows, it is automatically made larger. 34 * 35 * <p>Instances of <code>StringBuilder</code> are not safe for 36 * use by multiple threads. If such synchronization is required then it is 37 * recommended that {@link java.lang.StringBuffer} be used. 38 * 39 * @author Michael McCloskey 40 * @version %I%, %G% 41 * @see java.lang.StringBuffer 42 * @see java.lang.String 43 * @since 1.5 44 */
2.创建数组时,用new创建,参数可以是变量。
3.从代码中的图可以看出这类问题,后边的字符可以看成前8个字符平移得到的,也就是要解决这个问题只需解决前8个字符即可,后面的只需i%8。
4.代码中有除法和求余操作,一定要注意除数不要为0,除数为0的情况另行处理。这个问题中,当loop=0时,求得numRows,另行处理。
时间: 2024-10-11 16:29:45