g0001_0100.s0006_zigzag_conversion.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0006_zigzag_conversion;
// #Medium #String #2022_02_17_Time_4_ms_(92.78%)_Space_45_MB_(56.00%)
public class Solution {
public String convert(String s, int numRows) {
int sLen = s.length();
if (numRows == 1) {
return s;
}
int maxDist = numRows * 2 - 2;
StringBuilder buf = new StringBuilder();
for (int i = 0; i < numRows; i++) {
int index = i;
if (i == 0 || i == numRows - 1) {
while (index < sLen) {
buf.append(s.charAt(index));
index += maxDist;
}
} else {
while (index < sLen) {
buf.append(s.charAt(index));
index += maxDist - i * 2;
if (index >= sLen) {
break;
}
buf.append(s.charAt(index));
index += i * 2;
}
}
}
return buf.toString();
}
}