首页 > 试题广场 >

Z字形字符串

[编程题]Z字形字符串
  • 热度指数:10359 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

字符串"ZXYZXLISHIRING"写成3行的Z字形的样式如下:

Z   X   H   N
X Z L S I I G
Y   I   R


按行读这个Z字形图案应该是 "ZXHNXZLSIIGYIR"

请编写代码完成将字符串转化为指定行数的Z字形字符串:

示例1

输入

"AB",2

输出

"AB"
谁能告诉我,为啥注释代码会运行超时呢?就是第一行的字母是斜着写的管还是竖着写的管,差别很大吗?
public String convert (String s, int nRows) {
        int index=0;
        StringBuilder[] sb=new StringBuilder[nRows];
        for(int i=0;i<nRows;i++) {
            sb[i]=new StringBuilder();
        }
        for(int i=0;i<nRows;i++) {
            sb[i]=new StringBuilder();
        }
         int x=0;
        while(index<s.length()) {
            while(x<nRows&&index<s.length()) {
                sb[x++].append(s.charAt(index++));
            }
            x=nRows-2;
            while(x>0&&index<s.length()) {
                sb[x--].append(s.charAt(index++));
            }
            x=0;
            
            /*
            while(x>=0&&index<s.length()) {
                sb[x--].append(s.charAt(index++));
            }
            x=1;
            */
        }
        for(int i=1;i<nRows;i++) {
            sb[0].append(sb[i]);
        }
		return sb[0].toString();
    }


发表于 2021-02-07 23:14:10 回复(0)
/*举例子:1-20的数字,nRows = 5,输出结果应当为1 9 17 2 8 10 16 18 3 7 11 15 19 4 6 12 14 20 5 13
1           9              17
2        8  10         16  18
3     7     11      15     19
4  6        12  14         20
5           13
对于第一行和最后一行,每个数之间间隔2*nRows - 2

对于中间行而言:
第二行:(1)2,10,18之间仍然间隔2*nRows - 2
       (2)2,8之间间隔6,可表示为2*nRows - 2 - 2,同理对于10和16也是
第三行:(1)3,11,19之间间隔2*nRows - 2
       (2)3,7之间间隔4,可表示为2*nRows - 2 - 2*2,同理对于11和15也是
第三行:(1)4,12,20之间间隔2*nRows - 2
       (2)4,6之间间隔2,可表示为2*nRows - 2 - 2*3,同理对于12和14也是
       
通过发现以上规律,我们可以得到解题思路:
对于第一行和最后一行,通过简单的循环单独处理,一个个将字符加入到结果集中

对于中间行,每循环一次将每两个字符加入到结果集中,但是要判断字符是否存在越界访问情况
如上面例子第二行2和8,10和16,对于18,其加上间隔2*nRows - 2 - 2*2后字符访问越界
*/
public class Solution {
    public String convert(String s, int nRows) {
        if(s == null || s.length() == 0 || nRows <= 1)
            return s;
        int len = s.length();
        //使用StringBuffer运行效率更高
        StringBuffer res = new StringBuffer();
        //单独处理第一行
        for(int i = 0; i < len; i += 2*nRows - 2){
            res.append(s.charAt(i));
        }
        //处理中间行
         int k = 1;
         for(int i = 1; i < nRows - 1; i++){
              for(int j = i; j < len ; j += 2*nRows - 2){
                  res.append(s.charAt(j));
                  //判断字符访问是否越界
                  if((j + 2*nRows - 2 - 2*k) < len){
                      res.append(s.charAt(j + 2*nRows - 2 - 2*k));
                  } 
              }
             k++;
         }
         //单独处理最后一行
         for(int i = nRows - 1; i < len; i += 2*nRows - 2){
             res.append(s.charAt(i));
         }
         return res.toString();
    }
}

编辑于 2019-07-18 13:21:46 回复(0)
找到周期,然后循环将字符串中的字符输入到可变数组中,最后输入转换成字符串
class Solution {
    public String convert(String s, int numRows) {
        if(s==null || s.length()==0 || numRows<=1)
            return s;
        StringBuffer[] sb = new StringBuffer[numRows];
        int t = 2*numRows-2;
        for(int i=0;i<sb.length;i++)
            sb[i] = new StringBuffer();
        int len = s.length();
        int i = 0;
        while(i<len){
            for(int j = 0; j < numRows && i < len; j++)
                sb[j].append(s.charAt(i++));
            for(int j=numRows-2;j>0&&i<len;j--)
                sb[j].append(s.charAt(i++));
        }
        for(int j=1;j<numRows;j++)
            sb[0].append(sb[j]);
        return sb[0].toString();
    }
}

发表于 2019-01-03 09:26:35 回复(0)

public class Solution {
public String convert(String s, int nRows) {
if(nRows<=1)
return s;
char[] sa = s.toCharArray();
char[] res = new char[sa.length];
int k = 0;
for(int i = 0; i < nRows; i ++){
int m = i;
if(m == 0){
int n = m;
while(n < sa.length){
res[k] = sa[n];
k ++;
n += 2 nRows - 2;
}
}else if(m != nRows - 1){
int n = m;
int j = 0;
while(n < sa.length){
if(j % 2 == 0){
res[k] = sa[n];
n = n + 2
nRows - 2 i -2;
k ++;
j ++;
}else{
res[k] = sa[n];
n = n + 2
i;
k ++;
j ++;
}

            }
        }else{
            int n = m;
            while(n < sa.length){
                res[k] = sa[n];
                n += 2 * nRows - 2;
                k ++;
            }
        }

    }
    return String.valueOf(res);
}

}

发表于 2017-07-19 10:20:44 回复(0)

Create nRows StringBuffers, and keep collecting characters from original string to corresponding StringBuffer. Just take care of your index to keep them in bound.

public String convert(String s, int nRows) { char[] c = s.toCharArray(); int len = c.length;
    StringBuffer[] sb = new StringBuffer[nRows]; for (int i = 0; i < sb.length; i++) sb[i] = new StringBuffer(); int i = 0; while (i < len) { for (int idx = 0; idx < nRows && i < len; idx++) // vertically down sb[idx].append(c[i++]); for (int idx = nRows-2; idx >= 1 && i < len; idx--) // obliquely up sb[idx].append(c[i++]);
    } for (int idx = 1; idx < sb.length; idx++)
        sb[0].append(sb[idx]); return sb[0].toString();
}
发表于 2017-03-13 00:28:24 回复(0)