Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class Solution
- {
- public String[] zigzag(String text, int rows) {
- boolean down = true;
- String[] lines = new String[rows];
- Arrays.fill(lines, "");
- int print = 0;
- for (int i = 0; i < text.length(); i++) {
- for (int j = 0; j < rows; j++) {
- if (j == print) {
- lines[j] += text.charAt(i);
- } else {
- lines[j] += " ";
- }
- }
- if (down) {
- if ( (print + 1) >= rows ) {
- down = false;
- print--;
- } else {
- print++;
- }
- } else {
- if ( (print - 1) < 0 ) {
- down = true;
- print++;
- } else {
- print--;
- }
- }
- }
- return lines;
- }
- public static void main(String[] args)
- {
- Solution solution = new Solution();
- String[] lines = solution.zigzag("thisdemoofasuperlongtext", 4);
- for (int i = 0; i < lines.length; i++) {
- System.out.println(lines[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement