Skip to content

Commit

Permalink
solution: 0006. Zigzag Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
flynnpark committed Feb 1, 2024
1 parent 9d60ba8 commit 3529f5e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/0006/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
function convert(s: string, numRows: number): string {}
function convert(s: string, numRows: number): string {
if (numRows === 1) {
return s;
}

const rows = new Array(numRows).fill('');

let currentRow = 0;
let goingDown = true;

for (const char of s) {
rows[currentRow] += char;

if (currentRow === 0) {
goingDown = true;
} else if (currentRow === numRows - 1) {
goingDown = false;
}

currentRow += goingDown ? 1 : -1;
}

return rows.join('');
}

export { convert };

0 comments on commit 3529f5e

Please sign in to comment.