Zigzag Conversion

来源:互联网 发布:淘宝卖视频教程 犯法 编辑:程序博客网 时间:2024/06/06 05:22
public class Solution {    public String convert(String s, int numRows) {        if (s == null || numRows == 0) {            return null;        }        int length = s.length();        if (length <= numRows || numRows == 1) {            return s;        }        char[] ch = new char[length];        int count = 0;        int gap = 2 * (numRows - 1);        for (int i = 0; i < numRows; i++) {            int step1 = gap - 2 * i;            int step2 = 2 * i;            int range = i;            while (true) {                if (step1 != 0) {                    ch[count] = s.charAt(range);                    range += step1;                    count++;                }                if (range >= length || count >= length) {                    break;                }                if (step2 != 0) {                    ch[count] = s.charAt(range);                    range += step2;                    count++;                }                if (range >= length || count >= length) {                    break;                }            }        }        return new String(ch);    }}

0 0
原创粉丝点击