Leetcode#6||Zigzag Conversion

来源:互联网 发布:如何看java源码 编辑:程序博客网 时间:2024/06/06 02:41


public class Solution {    public String convert(String s, int numRows) {        if (numRows == 1 || s.length() <= numRows) {            return s;        }                StringBuilder sb = new StringBuilder();                int diff = (numRows - 1) * 2;                for (int i = 0; i < numRows; i++) {            if (i == 0 || i == numRows - 1) {                for (int j = i; j < s.length(); j += diff) {                    sb.append(s.charAt(j));                }            } else {                int j = i;                boolean condition = true;                int step1 = (numRows - i - 1) * 2;                int step2 = diff - step1;                                while (j < s.length()) {                    sb.append(s.charAt(j));                    if (condition) {                        j += step1;                    } else {                        j += step2;                    }                    condition = !condition;                }            }        }                return sb.toString();    }}


0 0
原创粉丝点击