leetcode — zigzag-conversion

/**
 * Source : https://oj.leetcode.com/problems/zigzag-conversion/
 *
 * Created by lverpeng on 2017/6/29.
 *
 *
 * 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"
 *
 * Write the code that will take a string and make this conversion given a number of rows:
 *
 * string convert(string text, int nRows);
 *
 * convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
 *
 */
public class ZigzagConvertion {

    /**
     * 将给定的字符串转换为指定行数的锯齿状,然后按行输出
     * 找出规律:
     * 第一行和最后一行,数组下标相差2 * nRows - 2
     * 中间的行:数组下表相差2 * nRows - 2 - 2 * i,i表示第i行
     * 要注意下标越界判断
     *
     * @param text
     * @param nRows
     * @return
     */
    public String convert (String text, int nRows) {
        int n = text.length();
        int base = 2 * nRows-  2;
        int index = 0;

        if (nRows == 1) {
            System.out.println(text);
        }

        String out = "";
        for (int i = 0; i < nRows; i++) {
            index = i;
            if (i == 0 || i == nRows - 1) {
                while (index < n) {
                    out += text.charAt(index);
                    index += base;
                }
            } else {
                while (index < n) {
                    out += text.charAt(index);
                    index += base - 2 * i;
                }
            }
        }
        return out;
    }

    /**
     * 实现准备好nRows的数组,遍历字符串,依次判断字符落在那一行,也就是哪一个数组
     *
     * @param text
     * @param nRows
     * @return
     */
    public String convertByIndex (String text, int nRows) {
        if(text.length() <= 1 || text.length() == nRows) {
            return text;
        }
        String[] lines = new String[nRows];
        int row = 0;
        int step = 0;
        for (int i = 0; i < text.length(); i++) {
            if (row == 0) {
                // row需要增加
                step = 1;
            }
            if (row == nRows - 1) {
                // row需要往回退
                step = -1;
            }
            lines[row] = lines[row] == null ? "" : lines[row];
            lines[row] += text.charAt(i);
            row += step;
        }

        String out = "";
        for (String line : lines) {
            out += line;
        }
        return out;
    }

    public static void main(String[] args) {
        ZigzagConvertion zigzagConvertion = new ZigzagConvertion();
        System.out.println(zigzagConvertion.convert("PAYPALISHIRING", 3));
        System.out.println(zigzagConvertion.convertByIndex("PAYPALISHIRING", 3));
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值