Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Solution
  4. {
  5. public String[] zigzag(String text, int rows) {
  6. boolean down = true;
  7. String[] lines = new String[rows];
  8. Arrays.fill(lines, "");
  9.  
  10. int print = 0;
  11. for (int i = 0; i < text.length(); i++) {
  12. for (int j = 0; j < rows; j++) {
  13. if (j == print) {
  14. lines[j] += text.charAt(i);
  15. } else {
  16. lines[j] += " ";
  17. }
  18. }
  19.  
  20. if (down) {
  21. if ( (print + 1) >= rows ) {
  22. down = false;
  23. print--;
  24. } else {
  25. print++;
  26. }
  27. } else {
  28. if ( (print - 1) < 0 ) {
  29. down = true;
  30. print++;
  31. } else {
  32. print--;
  33. }
  34. }
  35. }
  36.  
  37. return lines;
  38. }
  39.  
  40. public static void main(String[] args)
  41. {
  42. Solution solution = new Solution();
  43. String[] lines = solution.zigzag("thisdemoofasuperlongtext", 4);
  44. for (int i = 0; i < lines.length; i++) {
  45. System.out.println(lines[i]);
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement