-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.ZigzagConversion.java
More file actions
28 lines (23 loc) · 920 Bytes
/
Copy path6.ZigzagConversion.java
File metadata and controls
28 lines (23 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* 6: Zigzag Conversion
* https://leetcode.com/problems/zigzag-conversion/
*/
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1 || s.length() <= numRows) return s;
StringBuilder[] rows = new StringBuilder[numRows];
for (int i = 0; i < numRows; i++) rows[i] = new StringBuilder();
int row = 0; // Current row index
boolean goingDown = false; // Direction flag
for (char c : s.toCharArray()) {
rows[row].append(c); // Add char to current row
// Change direction if we reach top or bottom
if (row == 0 || row == numRows - 1) goingDown = !goingDown;
row += goingDown ? 1 : -1;
}
// Combine all rows into one string
StringBuilder result = new StringBuilder();
for (StringBuilder r : rows) result.append(r);
return result.toString();
}
}