Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- https://leetcode.com/problems/zigzag-conversion/
- Runtime: 3 ms, faster than 95.99% of Java online submissions for ZigZag Conversion.
- Memory Usage: 37.1 MB, less than 100.00% of Java online submissions for ZigZag Conversion.
- */
- class Solution {
- public String convert(String s, int numRows) {
- final int sl=s.length();
- if (numRows < 2 || sl <= numRows) {
- return s;
- }
- final int b = (numRows - 1) * 2;
- int j = 0, o1 = 0, o2 = 0;
- StringBuilder zz = new StringBuilder(sl);
- for (int i=0; i<numRows; ++i) {
- j = i;
- o1 = 2*i;
- o2 = b-o1;
- zz.append(s.charAt(j));
- while(j<sl) {
- if (o2 > 0) {
- j += o2;
- if (j<sl) {
- zz.append(s.charAt(j));
- }
- }
- if (o1 > 0) {
- j += o1;
- if (j<sl) {
- zz.append(s.charAt(j));
- }
- }
- }
- }
- return zz.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement