StromanStroman's solution


  • 0
    S

    It's done in java,showing below.
    I'm a chinese software engineer.
    If you are interested in it or have smarter solution,please contact me.
    My email is 13207533103@163.com

    public String convert(String sourceString,int rows) {
            if (rows < 1 || sourceString == null) {
                System.out.println("输入非法");
                return "";
            }
            StringBuilder resultBuilder = new StringBuilder();
            for (int counter = 0;counter < rows;counter++) {
                if (rows == 1) {
                    for (int counter0 = counter;counter0 < sourceString.length();counter0++) {
                        resultBuilder.append(sourceString.charAt(counter0));
                    }
                } else if (rows >= sourceString.length()) {
                    for (int counter0 = counter;counter0 < sourceString.length();counter0++) {
                        resultBuilder.append(sourceString.charAt(counter0));
                    }
                    break;
                } else {
                    int stepLength = 2 * rows - 2 - 2 * counter;
                    int counter0 = counter;
                    while (counter0 < sourceString.length()) {
                        resultBuilder.append(sourceString.charAt(counter0));
                        if (stepLength != 2 * rows - 2 && stepLength != 0) {
                            counter0 += stepLength;
                            stepLength = 2 * rows - 2 - stepLength;
                        }
                        else if (stepLength == 0) {
                            stepLength = 2 * rows - 2;
                            counter0 += stepLength;
                        } else {
                            counter0 += stepLength;
                        }
                    }
                }
            }
            return resultBuilder.toString();
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.