Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String convert(String s, int numRows) {
- if (s == null || numRows == 1 || s.length() <= numRows) {
- return s;
- }
- StringBuilder result = new StringBuilder();
- for (int row = 0; row < numRows; row++) {
- int step1 = (numRows - row - 1) * 2;
- int step2 = row * 2;
- int pos = row;
- result.append(s.charAt(pos));
- while (true) {
- pos += step1;
- if (pos >= s.length()) {
- break;
- }
- if (step1 > 0) {
- result.append(s.charAt(pos));
- }
- pos += step2;
- if (pos >= s.length()) {
- break;
- }
- if (step2 > 0) {
- result.append(s.charAt(pos));
- }
- }
- }
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement