Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. /*
  2. https://leetcode.com/problems/zigzag-conversion/
  3. Runtime: 3 ms, faster than 95.99% of Java online submissions for ZigZag Conversion.
  4. Memory Usage: 37.1 MB, less than 100.00% of Java online submissions for ZigZag Conversion.
  5. */
  6. class Solution {
  7.     public String convert(String s, int numRows) {
  8.         final int sl=s.length();
  9.         if (numRows < 2 || sl <= numRows) {
  10.             return s;
  11.         }
  12.        
  13.         final int b = (numRows - 1) * 2;
  14.         int j = 0, o1 = 0, o2 = 0;
  15.         StringBuilder zz = new StringBuilder(sl);
  16.         for (int i=0; i<numRows; ++i) {
  17.             j = i;
  18.             o1 = 2*i;
  19.             o2 = b-o1;
  20.             zz.append(s.charAt(j));
  21.             while(j<sl) {
  22.                 if (o2 > 0) {
  23.                     j += o2;
  24.                     if (j<sl) {
  25.                         zz.append(s.charAt(j));
  26.                     }
  27.                 }
  28.                 if (o1 > 0) {
  29.                     j += o1;
  30.                     if (j<sl) {
  31.                         zz.append(s.charAt(j));
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         return zz.toString();
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement