We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3721565 commit 264dd81Copy full SHA for 264dd81
java/6-Zigzag-Conversion.java
@@ -0,0 +1,31 @@
1
+//We check whether we are at the diagonal or not using a boolean and increment the pointer accordingly.
2
+
3
+class Solution {
4
+ public String convert(String s, int row) {
5
+ if (row==1) return s;
6
+ StringBuilder sb = new StringBuilder();
7
+ for (int i = 0; i<row; i++) {
8
+ int j = i;
9
+ if (i==0 || i==row-1) {
10
+ while (j<s.length()) {
11
+ sb.append(s.charAt(j));
12
+ j += 2*(row-1);
13
+ }
14
+ } else {
15
+ boolean diagonal = false;
16
17
+ if (!diagonal) {
18
19
+ j+=2*(row-i-1);
20
+ diagonal = true;
21
22
23
+ j+=2*i;
24
+ diagonal = false;
25
26
27
28
29
+ return sb.toString();
30
31
+}
0 commit comments