码迷,mamicode.com
首页 > 其他好文 > 详细

将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。

时间:2019-04-27 11:35:31      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:dex   row   str   for   index   length   stringbu   字符串   char   

public String convert(String s, int numRows) {
        if(numRows <= 1){
            return s;
        }
        StringBuilder[] sb = new StringBuilder[numRows];
        for(int i = 0; i < sb.length; i++){
            sb[i] = new StringBuilder();
        }
        for(int i=0; i < s.length(); i++){
            int index = i % (2*numRows -2);
            index = index < numRows?index : 2*numRows -2-index;
            sb[index].append(s.charAt(i));
        }
        for(int i = 1;i < sb.length; i++){
            sb[0].append(sb[i]);
        }
        return sb[0].toString();
    }

 

将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。

标签:dex   row   str   for   index   length   stringbu   字符串   char   

原文地址:https://www.cnblogs.com/q-1993/p/10778114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!